次のように、ユーザーとネストされたプロファイル クラスがあります。
class User < ActiveRecord::Base
has_one :profile
attr_accessible :profile_attributes
accepts_nested_attributes_for :profile
end
class Profile < ActiveRecord::Base
belongs_to :user
attr_accessible :name
end
user = User.find(1)
user.profile.id # => 1
user.update_attributes(profile_attributes: {name: 'some name'})
user.profile.id # => 2
Rails が古いプロファイルを破棄して新しいプロファイルを作成する理由がわかりません。
使用する
user.profile.update_attributes({name: 'some name'})
期待どおりに現在のプロファイルを更新するだけです。しかし、その場合、私はaccepts_nested_attributes_forを利用していません
更新がこのように行われる理由を誰かが知っていますか? どのユーザーにも接続されていないプロファイル行のデータベースになってしまうことは避けたいと思います。