この例では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の関連付けを持つ方法はありますか?