如何更新接受嵌套属性的深层嵌套模型?

时间:2014-07-10 19:44:23

标签: ruby-on-rails activerecord

我遇到的问题大多与this one相同。

我有三个模型叫做父,子,孙。

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 => [])

1 个答案:

答案 0 :(得分:1)

显然,这源于与在HABTM关系中使用现有ID相关的安全漏洞。参见:

https://groups.google.com/forum/#!topic/rubyonrails-core/uWQVCKqQMVU

https://groups.google.com/forum/?fromgroups#!topic/rubyonrails-security/-fkT0yja_gw

不会重新引入该功能

相关问题