如何更新接受嵌套属性的深层嵌套模型?
我有一个问题大多与此相同。
我有三个模特称他们为父,子,孙。
Parent has_many :children has_many :grandchildren, through: children
创建父项时,我通过集合复选框为其分配子项。 现在我需要更新父表单上的孙子关联。 我成立了
accepts_nested_attributes_for :children
我能够像我需要的那样更新属性。
当我尝试更新父记录时出现问题。 如果我删除一个孩子(通过取消它的复选框)我最终得到错误
ActiveRecord::RecordNotFound Couldn't find Child with ID=# for Parent with ID=#
即使这种关联在数据库中定义得很好。 经过进一步调查,我发现错误来自
activerecord (4.1.1) lib/active_record/nested_attributes.rb:545:in `raise_nested_attributes_record_not_found!'
有谁知道如何解决这个问题?
Ruby 1.9.3,Rails 4.1.1
在我的控制器中我有
params.require(:Parent).permit(:name, child_attributes: [:id, :grandchild_id], :child_ids => [])
显然,这源于与在HABTM关系中使用现有ID相关的安全漏洞。 看到:
https://groups.google.com/forum/#!topic/rubyonrails-core/uWQVCKqQMVU
https://groups.google.com/forum/?fromgroups#!topic/rubyonrails-security/-fkT0yja_gw
该function将不会重新引入