Tag: composite primary key

使用rails中的复合键更新模型嵌套属性

我有一个具有one_to_many关系的模型: 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” } } } ) 为什么会这样?