我有一种情况,即子项已构建但未保存,然后在视图中使用并引用父项。这导致了 Rails 记录缓存的广泛使用。我想让父母“渴望加载”未保存的儿童记录。
class Parent < ActiveRecord::Base
has_many :children
def make_children
loop..
children_array << children.build(...)
end
end
end
然后在视图中(注意“孩子”没有保存到数据库):
children_array.each do |child|
# What's the best way to optimise this so it doesn't
# keep selecting parent albeit from the cache?
child.parent
end