3

我想过滤 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

有没有办法“停用”隐式保存/更新?

4

1 回答 1

2

如果您most_loved_children可以收集为属性哈希集,那么您可以使用“

parent.children.build( most_loved_children )

这不会立即保存。

于 2013-10-04T15:35:13.003 回答