小编Cam*_*auu的帖子

Rails 嵌套属性尝试更新记录 ID

我有嵌套属性的问题,我有下一个实体:

class Experience < ActiveRecord::Base
  has_many :roles, inverse_of: :experience
end

class Role < ActiveRecord::Base
  belongs_to :experience, inverse_of: :roles
end
Run Code Online (Sandbox Code Playgroud)

我有下一组参数:

 params = {"end_date"=>"02/01/2012",
 "city"=>"Buenos Aires",
 "state"=>"",
 "country"=>"",
 "title"=>nil,
 "company"=>"Restorando",
 "current"=>nil,
 "description"=>nil,
 "roles_attributes"=>
  [{"id"=>558, "title"=>"System Owner", "start_date"=>"03/01/2000", "end_date"=>"02/01/2001", "description"=>"<p>I did a bunch of stuff</p>", "current"=>false},
   {"id"=>561, "title"=>"Test", "description"=>nil, "current"=>nil},
   {"id"=>557, "title"=>"Full Stack Developerr", "start_date"=>"01/01/2011", "end_date"=>"02/01/2012", "description"=>"<p>Full Stack Developer description</p>", "current"=>false}],
 "resumes_experiences_attributes"=>[{"id"=>384, "resume_id"=>809}, {"id"=>385, "resume_id"=>3199}]}
Run Code Online (Sandbox Code Playgroud)

我想更新一个 Rails 模型。所以我执行:

@experience = Experience.find(some_id)
@experience.update_attributes(params)
Run Code Online (Sandbox Code Playgroud)

这给我返回一个错误

ActiveRecord::RecordNotUnique:PG::UniqueViolation:错误:重复的键值违反了唯一约束“roles_pkey”

如果我看到 Rails 试图更新什么,我会看到下一条sql语句:

UPDATE“角色”SET“start_date”= $1,“end_date”= $2,“current”= …

ruby-on-rails nested-attributes

5
推荐指数
0
解决办法
338
查看次数

标签 统计

nested-attributes ×1

ruby-on-rails ×1