如何更新深度嵌套模式,接受嵌套的属性? [英] How to update deeply nested models that accept nested attributes?
问题描述
我有一个问题几乎等于<一个href="http://stackoverflow.com/questions/20653519/couldnt-find-model-with-id-14-for-property-with-id-1#comment38270153_20653519">this 之一。
我有三个型号叫他们父母,子女,孙子女。
父
的has_many:儿童
的has_many:孙子,通过:儿童
在创建一个家长,我通过收集复选框分配的孩子吧。现在我需要更新父窗体上孙子关联。我设置了
accepts_nested_attributes_for:儿童
和我能就像我需要更新的属性。
当我尝试更新父记录的问题就出现了。如果我删除一个孩子(通过unclicking它的复选框),我最终的误差
的ActiveRecord :: RecordNotFound
找不到孩子与ID =#为家长与ID =#
尽管这种关联在数据库中明确界定。在进一步的调查,我发现了错误,从
未来 的ActiveRecord(4.1.1)的lib / active_record / nested_attributes.rb:545:在'raise_nested_attributes_record_not_found!
有谁知道如何解决这个问题?
红宝石1.9.3,Rails的4.1.1
在我的控制器我有
params.require(:家长).permit(:姓名,child_attributes:[:ID,:grandchild_id]:child_ids =&GT; [])
显然,这源于与使用现有的IDS在HABTM关系的一个安全漏洞。参见:
https://groups.google.com/forum/#!主题/ rubyonrails核心/ uWQVCKqQMVU
的https://groups.google.com/forum/?fromgroups#!topic/rubyonrails-security/-fkT0yja_gw
该功能将不会被重新
I have a problem mostly identical to this one.
I have three models call them Parent, Child, Grandchild.
Parent
has_many :children
has_many :grandchildren, through: children
When creating a parent, I assign children to it via collection check boxes. Now I need to update grandchildren association on the parent form. I set up
accepts_nested_attributes_for :children
and I am able to update the attributes just like I need to.
The problem arises when I attempt to update a parent record. If I remove a child (through unclicking it's checkbox) I end up with the error
ActiveRecord::RecordNotFound
Couldn't find Child with ID=# for Parent with ID=#
even though this association is well defined in the database. On further investigation I found out the error was coming from
activerecord (4.1.1) lib/active_record/nested_attributes.rb:545:in `raise_nested_attributes_record_not_found!'
Does anyone know how to solve this problem?
Ruby 1.9.3, Rails 4.1.1
In my controller I have
params.require(:Parent).permit(:name, child_attributes: [:id, :grandchild_id], :child_ids => [])
Apparently this arises from a security hole related to using existing ids in HABTM relationships. See:
https://groups.google.com/forum/#!topic/rubyonrails-core/uWQVCKqQMVU
https://groups.google.com/forum/?fromgroups#!topic/rubyonrails-security/-fkT0yja_gw
The functionality will not be reintroduced
这篇关于如何更新深度嵌套模式,接受嵌套的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!