# Pre-load second level association?

**URL:** <https://www.ruby-forum.com/t/pre-load-second-level-association/135104>\
**Category:** Rails\
**Created:** [April 17, 2008, 10:20pm UTC](https://www.ruby-forum.com/t/pre-load-second-level-association/135104 "2008-04-17T22:20:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [April 17, 2008, 10:20pm UTC](https://www.ruby-forum.com/t/pre-load-second-level-association/135104/1 "2008-04-17T22: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:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [April 17, 2008, 10:26pm UTC](https://www.ruby-forum.com/t/pre-load-second-level-association/135104/2 "2008-04-17T22:26:50Z")

</div>

On 17 Apr 2008, at 21:20, Jonathan R. wrote:

> 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:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [April 18, 2008, 12:06am UTC](https://www.ruby-forum.com/t/pre-load-second-level-association/135104/3 "2008-04-18T00:06:31Z")

</div>

Jonathan R. wrote:

> 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.5valleys.com/)

[http://www.workingwithrails.com/person/8078](http://www.workingwithrails.com/person/8078)

---

<div class="post-metadata">

**Author:** ![jrochkind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jrochkind](https://www.ruby-forum.com/u/jrochkind)\
**Post date:** [April 18, 2008, 12:11am UTC](https://www.ruby-forum.com/t/pre-load-second-level-association/135104/4 "2008-04-18T00:11:32Z")

</div>

Thanks all, that did the trick. Good to know that works with an  
association find too, nice.

Jon G. 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.
