我想过滤 API 控制器中的嵌套关联,然后重新分配过滤后的关联,只有过滤后的关联才会从 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
有没有办法“停用”隐式保存/更新?