Ruby on rails 在rails中使用复合键更新模型嵌套属性

Ruby on rails 在rails中使用复合键更新模型嵌套属性,ruby-on-rails,activerecord,nested-attributes,composite-key,composite-primary-key,Ruby On Rails,Activerecord,Nested Attributes,Composite Key,Composite Primary Key,我有一个一对多关系的模型: 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_holde

我有一个一对多关系的模型:

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

为什么会发生这种情况?

您需要传递集合对象id,如下所示:

work.update(
  {"work_right_holders_attributes"=>
    {
     "0"=>{ "role"=>"Author", 
            "right_holder_id"=>"2", 
            "work_id"=>work.id, 
            "share"=>"11",
            "id" => [work.id, "2", "Author"]
           }
     }
  }
)
这应该行得通


obs:在Rails 4.1.1中有一个bug,但这不起作用,但在Rails 4.2.1中它起作用了

您现在如何更新?我向你展示的语法是这样的?
work.update(
  {"work_right_holders_attributes"=>
    {
     "0"=>{ "role"=>"Author", 
            "right_holder_id"=>"2", 
            "work_id"=>work.id, 
            "share"=>"11",
            "id" => [work.id, "2", "Author"]
           }
     }
  }
)