Rails嵌套属性_destroy

时间:2017-03-30 22:36:51

标签: ruby-on-rails activerecord nested-attributes destroy

我有3个模特

class Client < ApplicationRecord
  has_many :offices
  has_many :contact

  accepts_nested_attributes_for :offices, :contact, allow_destroy: true
end


class Contact < ApplicationRecord
  belongs_to :client
end

class Office < ApplicationRecord
  belongs_to :client
end

我有一个嵌套的表单,除了删除外,它的工作正常。当我为嵌套记录(办公室或联系人)设置_destroy为true时,以及嵌套记录,父记录(客户端)也会被删除。

这是我的控制器:

@client = Client.find(params[:id])
@client.update_attributes( update_params )

以下是传递给更新操作的update_params:

{"name"=>"Anderson-Hahn", 
  "code"=>"87-0931663", 
  "projects_count"=>nil, 
  "user_id"=>"3", 
  "contacts_attributes"=>[
    {"id"=>"13", "first_name"=>"Brayan", "last_name"=>"Fritsch", "email"=>"lora@erdman.name", "w_phone"=>"(257) 752-2245 x261", "m_phone"=>"1-642-424-2725", "phone"=>"(762) 599-6951 x200", "linkedin"=>"sample-linkedin", "skype"=>"someskypeaccount", "position"=>"some position", "_destroy"=>false}, 
    {"id"=>"14", "first_name"=>"Leonardo", "last_name"=>"Auer", "email"=>"ricky@beerwalter.co", "w_phone"=>"1-254-525-3165 x3900", "m_phone"=>"501-206-2337", "phone"=>"1-913-256-5757 x3396", "linkedin"=>"sample-linkedin", "skype"=>"someskypeaccount", "position"=>"some position", "_destroy"=>false}
  ], 
  "offices_attributes"=>[
    {"id"=>"13", "country"=>"Syrian Arab Republic", "city"=>"West Ambrose", "address"=>"Vicky Via", "lat"=>"61.006562395442444", "lon"=>"61.681150392689545", "created_at"=>"2017-03-14T15:44:59.000Z", "_destroy"=>false},
    {"id"=>"14", "country"=>"Venezuela", "city"=>"Gerlachside", "address"=>"Herzog Mill", "lat"=>"-4.938697606908136", "lon"=>"7.983714813706257", "created_at"=>"2017-03-14T15:44:59.000Z", "_destroy"=>true}
  ]
}

到目前为止我无法弄明白......有什么想法吗?

0 个答案:

没有答案