我在學習Rails,所以這是一個非常簡單的問題。簡單的ActiveRecord關聯問題
我試圖關聯用戶< - 在經典的一對多風格用戶自己的帖子中的郵件。
這裏是我的模型:
class Post < ActiveRecord::Base
attr_accessible :body
belongs_to :user
end
class User < ActiveRecord::Base
has_many :posts
attr_accessible :email, :password, :password_confirmation
end
我也創造了必要的遷移:
class AddUserIdToPosts < ActiveRecord::Migration
def self.up
add_column :posts, :user_id, :integer
end
def self.down
remove_column :posts, :user_id
end
end
我面對的是試圖讓導軌看到這一切,並建立正確的問題協會。
當我打電話:
@user = User.first
@user.post.build
我得到一個
NoMethodError: undefined method `post' for #<User:0x10319fbc8>
有什麼我錯過了?