class User < ActiveRecord::Base
has_many :microposts
end
class Mircopost < ActiveRecord::Base
belongs_to :user
end
ActiveRecord::Schema.define(:version => 20120216035330) do
create_table "mircoposts", :force => true do |t|
t.integer "user_id"
t.string "content"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
end
我運行rake db:migrate,rails console並創建一個假用戶= User.create(:name =>「abc」)。但是,當我運行user.microposts.empty?時,它會產生「NameError:未初始化的常量User :: Micropost」。所以我不明白爲什麼這些方法是自動生成的。 在此先感謝!使用has_many後沒有創建關聯
microposts或mircoposts ... one is mispelled – drhenner 2012-02-16 04:22:06