# pre-load second level association?

**URL:** <https://discuss.rubyonrails.org/t/pre-load-second-level-association/28180>\
**Category:** rubyonrails-talk\
**Created:** [April 17, 2008, 8:20pm UTC](https://discuss.rubyonrails.org/t/pre-load-second-level-association/28180 "2008-04-17T20:20:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [April 17, 2008, 8:20pm UTC](https://discuss.rubyonrails.org/t/pre-load-second-level-association/28180/1 "2008-04-17T20:20:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [April 17, 2008, 8:25pm UTC](https://discuss.rubyonrails.org/t/pre-load-second-level-association/28180/2 "2008-04-17T20:25:46Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![J\_Garvin](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@J\_Garvin](https://discuss.rubyonrails.org/u/J_Garvin)\
**Post date:** [April 17, 2008, 10:05pm UTC](https://discuss.rubyonrails.org/t/pre-load-second-level-association/28180/3 "2008-04-17T22:05:36Z")

</div>

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 &nbsp;&nbsp; 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.
