【发布时间】:2009-12-08 19:38:08
【问题描述】:
我有一种情况,子级已构建但未保存,然后在视图中使用并引用父级。这导致了 Rails 记录缓存的广泛使用。我想让父母“渴望加载”未保存的子记录。
class Parent < ActiveRecord::Base
has_many :children
def make_children
loop..
children_array << children.build(...)
end
end
end
然后在视图中(注意'child'没有保存到DB):
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
【问题讨论】:
-
注意:我在这里使用了术语预加载来表示我所追求的,尽管我知道在这种情况下预加载是不可能的,因为它不是从数据库中获取的,因此可以'不要像通常的急切加载那样使用连接。
标签: ruby-on-rails activerecord eager-loading