在此示例中,我创建了一个userno profile,然后profile为该用户创建了一个。我尝试将 build 与has_one关联一起使用,但结果失败了。我看到这项工作的唯一方法是使用has_many. user应该最多只有一个profile。
我一直在尝试这个。我有:
class User < ActiveRecord::Base
has_one :profile
end
class Profile < ActiveRecord::Base
belongs_to :user
end
但是当我这样做时:
user.build_profile
我得到错误:
ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'profiles.user_id' in 'where clause': SELECT * FROM `profiles` WHERE (`profiles`.user_id = 4) LIMIT 1
Rails 中有没有办法拥有 0 或 1 个关联?