对于分配ActiveRecord模型协会没有自动保存 [英] Assign association for Activerecord model without automatic save
本文介绍了对于分配ActiveRecord模型协会没有自动保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在一个API控制过滤嵌套的关联,并重新分配过滤协会事后,这只是过滤的联想将从API返回。
I'd like to filter a nested association within an API controller and reassign the filtered association afterwards, that just the filtered association will be return from the API.
class Parent < ActiveRecord::Base
has_many :children
end
class Child < ActiveRecord::Base
belongs_to :parent
end
parent = Parent.find(1)
most_loved_children = []
# do some filtering
parent.children = most_loved_children # triggers save on each of the children
parent.to_json # should only return parent with most_loved_children
有没有办法为禁用隐保存/更新?
Is there a way to "deactivate" the implicit save/updates?
编辑:新问题
New question
推荐答案
如果你的 most_loved_children
可以被收集为一组属性的哈希值,那么你可以使用
If your most_loved_children
can be collected as sets of attribute hashes, then you can use"
parent.children.build( most_loved_children )
这不会做即时保存。
Which will not do an immediate save.
这篇关于对于分配ActiveRecord模型协会没有自动保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文