0
我有one_to_many relatioship模型:在軌複合鍵更新模型嵌套屬性
class Work < ActiveRecord::Base
has_many :work_right_holders
accepts_nested_attributes_for :work_right_holders, allow_destroy: true
end
class WorkRightHolder < ActiveRecord::Base
self.primary_keys = :work_id, :right_holder_id, :role
belongs_to :work
belongs_to :right_holder
end
當我嘗試更新嵌套屬性的work
,它在關係創建對象的新實例,而不是根據其主鍵更新現有產品:
work.update(
{"work_right_holders_attributes"=>
{
"0"=>{ "role"=>"Author",
"right_holder_id"=>"2",
"work_id"=>work.id,
"share"=>"11"
}
}
}
)
爲什麼會發生這種情況?
你怎麼現在更新?我以這種方式向你展示了語法? –