# Issue w/ Eager Loading

**URL:** <https://www.ruby-forum.com/t/issue-w-eager-loading/106634>\
**Category:** Rails\
**Created:** [June 12, 2007, 5:14pm UTC](https://www.ruby-forum.com/t/issue-w-eager-loading/106634 "2007-06-12T17:14:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [June 12, 2007, 5:14pm UTC](https://www.ruby-forum.com/t/issue-w-eager-loading/106634/1 "2007-06-12T17:14:38Z")

</div>

Hi,

I have a rails app that uses these models:

class CoreRouter \< ActiveRecord::Base  
has\_many :ports  
end

class Port \< ActiveRecord::Base  
belongs\_to :core\_router  
has\_many :addresses  
end

class Address \< ActiveRecord::Base  
belongs\_to :port  
end

I have one main list page that shows all CoreRouters, the associated  
ports and the associated addresses. The problem is that I now have over  
400 addresses and I’m seeing over 400 queries when I generate this page,  
taking about 10 seconds to load. I thought an answer may be Eager  
Loading:

@core\_routers = CoreRouter.find(:all, :include =\> [ :ports, :addresses  
])

But that fails:

ActiveRecord::ConfigurationError (Association named ‘addresses’ was not  
found; perhaps you misspelled it?):

I’m assuming this is because the CoreRouter class doesn’t have a direct  
association with the Address class.

How can I get it to eagerly load the addresses that are associated with  
the ports?

---

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [June 12, 2007, 6:29pm UTC](https://www.ruby-forum.com/t/issue-w-eager-loading/106634/2 "2007-06-12T18:29:33Z")

</div>

> has\_many :addresses  
> Loading:  
> association with the Address class.
> 
> How can I get it to eagerly load the addresses that are associated with  
> the ports?

@core\_routers = CoreRouter.find(:all,  
:include =\> {:ports =\> :addresses})

(it’s either that or…)

:include =\> :ports =\> :addresses)

I can never remember as well I’ve never actually done it 🙂

---

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [June 12, 2007, 7:21pm UTC](https://www.ruby-forum.com/t/issue-w-eager-loading/106634/3 "2007-06-12T19:21:41Z")

</div>

On Jun 12, 2007, at 12:41 PM, Philip H. wrote:

> > has\_many :addresses  
> > page,  
> > was not  
> > @core\_routers = CoreRouter.find(:all,  
> > :include =\> {:ports =\> :addresses})
> 
> (it’s either that or…)

Yeah, it’s that.

and if you need to include a :conditions option when you  
have :include’s, you need to indicate the table such as  
:conditions =\> [ ‘core\_routers.installed\_on \< ? AND ports.in\_use = ?’,  
1.year.ago.beginning\_of\_day.to\_s(:db), true ]

-Rob

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

---

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [June 12, 2007, 8:16pm UTC](https://www.ruby-forum.com/t/issue-w-eager-loading/106634/4 "2007-06-12T20:16:41Z")

</div>

Philip H. wrote:

> @core\_routers = CoreRouter.find(:all,  
> :include =\> {:ports =\> :addresses})

That worked great… Dropped my DB time down from about 2 seconds to  
less than .5 seconds…

---

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [June 12, 2007, 8:18pm UTC](https://www.ruby-forum.com/t/issue-w-eager-loading/106634/5 "2007-06-12T20:18:16Z")

</div>

Rob B. wrote:

> and if you need to include a :conditions option when you  
> have :include’s, you need to indicate the table such as  
> :conditions =\> [ ‘core\_routers.installed\_on \< ? AND ports.in\_use = ?’,  
> 1.year.ago.beginning\_of\_day.to\_s(:db), true ]

Thanks Rob - I don’t need this yet, but might!
