Let''s say I have Customer -> Invoice -> OrderItem. All of those are one-to-many. Is there any way I can tell ActiveRecord than when I access some_customer.invoices, AR should pre-load all the invoice.order_items too, in one query, using a join? Similar to if I issued an :include directive in a #find, but I''m not doing a manual find, I''m just accesing some_customer.invoice. I want AR to automatically pre-load all of the invoice''s order_items every time any code accesses some_customer.invoice. Is this something AR can do? If not, is there a common workaround to get this efficiency? Is there a callback hook for after the customer.invoices are loaded? Thanks for any advice! Jonathan -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 17 Apr 2008, at 21:20, Jonathan Rochkind wrote:> > Let''s say I have Customer -> Invoice -> OrderItem. All of those are > one-to-many. > > Is there any way I can tell ActiveRecord than when I access > some_customer.invoices, AR should pre-load all the invoice.order_items > too, in one query, using a join? Similar to if I issued an :include > directive in a #find, but I''m not doing a manual find, I''m just > accesing > some_customer.invoice. I want AR to automatically pre-load all of the > invoice''s order_items every time any code accesses > some_customer.invoice. > >you can say has_many :foos, :include => :bars Fred> Thanks for any advice! > > Jonathan > -- > Posted via http://www.ruby-forum.com/. > > --~--~---------~--~----~------------~-------~--~----~ > You received this message because you are subscribed to the Google > Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > For more options, visit this group at http://groups.google.com/grou--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jonathan Rochkind wrote:> Let''s say I have Customer -> Invoice -> OrderItem. All of those are > one-to-many. > > Is there any way I can tell ActiveRecord than when I access > some_customer.invoices, AR should pre-load all the invoice.order_items > too, in one query, using a join? Similar to if I issued an :include > directive in a #find, but I''m not doing a manual find, I''m just accesing > some_customer.invoice. I want AR to automatically pre-load all of the > invoice''s order_items every time any code accesses > some_customer.invoice. > > Is this something AR can do? If not, is there a common workaround to > get this efficiency? Is there a callback hook for after the > customer.invoices are loaded? > > Thanks for any advice! > > Jonathan >in your Customer class... has_many :invoices, :include => :order_items Now, whenever you have @costumer, and you call @customer.invoices, then the order_items will be eagerly loaded. You''re probably going to do something more like @customer.invoices.find(params[:id]), and the above will work with that too. -- http://www.5valleys.com/ http://www.workingwithrails.com/person/8078 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks all, that did the trick. Good to know that works with an association find too, nice. Jon Garvin wrote:>> Is there a callback hook for after the >> customer.invoices are loaded? >> > in your Customer class... > > has_many :invoices, :include => :order_items > > Now, whenever you have @costumer, and you call @customer.invoices, then > the order_items will be eagerly loaded. You''re probably going to do > something more like @customer.invoices.find(params[:id]), and the above > will work with that too. >-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---