【发布时间】:2011-01-29 04:50:16
【问题描述】:
在此示例中,我创建了一个没有 profile 的 user,然后为该用户创建了一个 profile。我尝试使用带有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 关联?
【问题讨论】:
-
您到底尝试了什么?能否请您发布一些代码?
标签: ruby-on-rails associations has-one database-relations