在这个例子中,我创建了一个user
没有profile
,然后在profile
为该用户创建一个.我尝试使用带有has_one
关联的构建,但是爆炸了.我看到这个工作的唯一方法是使用has_many
.本user
应该只有最多只能有一个profile
.
我一直在尝试这个.我有:
class User < ActiveRecord::Base
has_one :profile
end
class Profile < ActiveRecord::Base
belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)
但当我这样做时:
user.build_profile
Run Code Online (Sandbox Code Playgroud)
我收到错误:
ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'profiles.user_id' in 'where clause': SELECT * FROM `profiles` WHERE (`profiles`.user_id = 4) LIMIT 1
Run Code Online (Sandbox Code Playgroud)
在rails中有一种方法可以有0或1个关联吗?