Rails ActiveRecord - eager loading (sort of) when using 'build' from has_many
I have a situation where children are built but not saved, and are then being used in the view with references to the parent. This leads to extensive use of rails record caching. I'd like to have the parent 'eager loaded' with the unsaved children records.
class Parent < ActiveRecord::Base
has_many :children
def make_children
loop..
children_array << children.build(...)
end
end
end
Then in the view (note that 'child' isn't saved to DB):
children_array.each do |child|
# What's the best way to optimise this so it doesn't
# keep selecting parent a开发者_如何学Golbeit from the cache?
child.parent
end
I'm not sure I understand the problem. The query cache is your friend...! You're calls to .parent
aren't hitting the database.
If you can include more information as to what you're trying to do it might be easier to help with the problem.
精彩评论