views:

231

answers:

1

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 albeit from the cache?
  child.parent 
end
A: 

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.

jonnii
Thanks jonnii - you're right... 'The query cache is your friend'. I was sort of going off on a tangent and thought I'd post a question to stop myself. Is there not any significant overhead worth worrying about with the query cache then?
Jo
I don't think so not. It's definitely better than actually hitting the db.
jonnii