# Best way to eager load accross multiple relationships?

**URL:** <https://www.ruby-forum.com/t/best-way-to-eager-load-accross-multiple-relationships/111113>\
**Category:** Rails\
**Created:** [August 2, 2007, 7:38pm UTC](https://www.ruby-forum.com/t/best-way-to-eager-load-accross-multiple-relationships/111113 "2007-08-02T19:38:55Z")\
**Posts on this page:** 5\
**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:** [August 2, 2007, 7:38pm UTC](https://www.ruby-forum.com/t/best-way-to-eager-load-accross-multiple-relationships/111113/1 "2007-08-02T19:38:55Z")

</div>

Let’s say I have Departments, which have many Persons , which have many  
Pets.

I can fetch Departments and eagerly load their Persons, with :include=\>  
in the find statement.

But what if I want to eagerly load all the Persons, _and_ all of the  
Persons’ Pets, in, if not one single db trip, then maybe in two? In as  
few as possible.

I don’t think I can do this with stock :include. Does anyone have any  
advice as to how I might be able to accomplish this?

Thanks,  
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:** [August 2, 2007, 7:45pm UTC](https://www.ruby-forum.com/t/best-way-to-eager-load-accross-multiple-relationships/111113/2 "2007-08-02T19:45:56Z")

</div>

you’ll want to read this…  
[http://snippets.dzone.com/posts/show/2089](http://snippets.dzone.com/posts/show/2089)

On Aug 2, 1:38 pm, Jonathan R. \<rails-mailing-l…@andreas-

---

<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:** [August 2, 2007, 8:04pm UTC](https://www.ruby-forum.com/t/best-way-to-eager-load-accross-multiple-relationships/111113/3 "2007-08-02T20:04:01Z")

</div>

Wow, awesome, perfect! I had no idea a nested include was possible. That  
solves so many things.

[If I wanted to actually document this, and submit it as a patch to the  
official documentation… how would I do this?]

vincent fazio wrote:

> you’ll want to read this…  
> [http://snippets.dzone.com/posts/show/2089](http://snippets.dzone.com/posts/show/2089)
> 
> On Aug 2, 1:38 pm, Jonathan R. \<rails-mailing-l…@andreas-

---

<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:** [August 2, 2007, 9:05pm UTC](https://www.ruby-forum.com/t/best-way-to-eager-load-accross-multiple-relationships/111113/4 "2007-08-02T21:05:11Z")

</div>

On Aug 2, 2007, at 2:04 PM, Jonathan R. wrote:

> Wow, awesome, perfect! I had no idea a nested include was possible.  
> That  
> solves so many things.
> 
> [If I wanted to actually document this, and submit it as a patch to  
> the  
> official documentation… how would I do this?]

[http://api.rubyonrails.org/classes/ActiveRecord/Associations/](http://api.rubyonrails.org/classes/ActiveRecord/Associations/)  
ClassMethods.html

> vincent fazio wrote:
> 
> > you’ll want to read this…  
> > [http://snippets.dzone.com/posts/show/2089](http://snippets.dzone.com/posts/show/2089)

That’s where the documentation changes would go, but you’d open a  
ticket to add your [PATCH].

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<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:** [August 2, 2007, 9:05pm UTC](https://www.ruby-forum.com/t/best-way-to-eager-load-accross-multiple-relationships/111113/5 "2007-08-02T21:05:11Z")

</div>

On 8/2/07, Jonathan R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [If I wanted to actually document this, and submit it as a patch to the  
> official documentation… how would I do this?]

watch this screen cast

> **[\#50 Contributing to Rails - RailsCasts](http://railscasts.com/episodes/50-contributing-to-rails)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

–  
Edward Grant  
removed\_email\_address@domain.invalid

RPGA #342155  
CAMARILLA #US2002022579
