# Eager loading multi-level belongs\_to

**URL:** <https://www.ruby-forum.com/t/eager-loading-multi-level-belongs-to/137934>\
**Category:** Rails\
**Created:** [May 18, 2008, 3:03pm UTC](https://www.ruby-forum.com/t/eager-loading-multi-level-belongs-to/137934 "2008-05-18T15:03:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nanosk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/a4c791/32.png) [@nanosk](https://www.ruby-forum.com/u/nanosk)\
**Post date:** [May 18, 2008, 3:03pm UTC](https://www.ruby-forum.com/t/eager-loading-multi-level-belongs-to/137934/1 "2008-05-18T15:03:11Z")

</div>

Hi,

I am working on a new project (Rails 2.0.2) and this is driving me nuts.  
I would appreciate any help!

I have the following classes:

class Nation \< ActiveRecord::Base  
has\_many :states  
end

class State \< ActiveRecord::Base  
has\_many :cities  
belongs\_to :nation  
end

class City \< ActiveRecord::Base  
has\_many :events  
belongs\_to :state  
end

class Event \< ActiveRecord::Base  
belongs\_to :city  
end

The chain for this is event.city.state.nation. When fetching all the  
events I want to eager-load the belongs\_to associations from the  
database.

I am trying to do this:  
Event.find(:all, :include =\> [{:nation =\>{:city =\> :state}}]

I get this:  
Association named ‘nation’ was not found; perhaps you misspelled it?

I would greatly appreciate any help.

Thanks,  
Sascha

---

<div class="post-metadata">

**Author:** ![nanosk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/a4c791/32.png) [@nanosk](https://www.ruby-forum.com/u/nanosk)\
**Post date:** [May 18, 2008, 3:05pm UTC](https://www.ruby-forum.com/t/eager-loading-multi-level-belongs-to/137934/2 "2008-05-18T15:05:27Z")

</div>

I forgot to mention, doing this one level deep works:

Event.find(:all, :include =\> [{:city =\> :state}]

---

<div class="post-metadata">

**Author:** ![nanosk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/a4c791/32.png) [@nanosk](https://www.ruby-forum.com/u/nanosk)\
**Post date:** [May 18, 2008, 5:17pm UTC](https://www.ruby-forum.com/t/eager-loading-multi-level-belongs-to/137934/3 "2008-05-18T17:17:03Z")

</div>

On 18 May 2008, at 14:03, Sascha Konietzke wrote:

> has\_many :states  
> end  
> Event.find(:all, :include =\> [{:nation =\>{:city =\> :state}}]

That tries to eager load the nation association from event, event  
doesn’t have such an association (or in other words, you’re doing  
things backwards)

Something along the lines of :include =\> {:city =\> {:state  
=\> :nation}} should do what you want.

Fred

---

<div class="post-metadata">

**Author:** ![nanosk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/a4c791/32.png) [@nanosk](https://www.ruby-forum.com/u/nanosk)\
**Post date:** [May 22, 2008, 11:40am UTC](https://www.ruby-forum.com/t/eager-loading-multi-level-belongs-to/137934/4 "2008-05-22T11:40:15Z")

</div>

Frederick C. wrote:

> On 18 May 2008, at 14:03, Sascha Konietzke wrote:
> 
> > has\_many :states  
> > end  
> > Event.find(:all, :include =\> [{:nation =\>{:city =\> :state}}]
> 
> That tries to eager load the nation association from event, event  
> doesn’t have such an association (or in other words, you’re doing  
> things backwards)
> 
> Something along the lines of :include =\> {:city =\> {:state  
> =\> :nation}} should do what you want.
> 
> Fred

Hi Fred,

it works now. Many Thanks!

Sascha
