この例ではuser、 noを使用して をprofile作成し、後でprofileそのユーザー用に を作成します。関連付けを使用してビルドを使用しようとしましたhas_oneが、失敗しました。これが機能していることを確認する唯一の方法は、を使用することhas_manyです。にはuser最大で 1 つしかないはず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
レールに0または1の関連付けを持つ方法はありますか?