# Eager loading or Lazy loading

**URL:** <https://www.ruby-forum.com/t/eager-loading-or-lazy-loading/133783>\
**Category:** Rails\
**Created:** [April 3, 2008, 3:57pm UTC](https://www.ruby-forum.com/t/eager-loading-or-lazy-loading/133783 "2008-04-03T15:57:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![selva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/13edae/32.png) [@selva](https://www.ruby-forum.com/u/selva)\
**Post date:** [April 3, 2008, 3:57pm UTC](https://www.ruby-forum.com/t/eager-loading-or-lazy-loading/133783/1 "2008-04-03T15:57:07Z")

</div>

Hai…

Whether ruby on rails is eager loading or lazy loading???

I think while using association in model its getting eager loading…

can any one explain more???

---

<div class="post-metadata">

**Author:** ![selva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/13edae/32.png) [@selva](https://www.ruby-forum.com/u/selva)\
**Post date:** [April 3, 2008, 4:03pm UTC](https://www.ruby-forum.com/t/eager-loading-or-lazy-loading/133783/2 "2008-04-03T16:03:55Z")

</div>

On 3 Apr 2008, at 14:57, Selvaraj S. wrote:

> Hai…
> 
> Whether ruby on rails is eager loading or lazy loading???
> 
> I think while using association in model its getting eager loading…

Assuming I do bob = Person.find(124) and then do something with  
bob.orders, then the association is lazy loaded, it’s not loaded until  
you touch it. You can also eager load: bob = Person.find(124, :include  
=\> ‘orders’)

Fred

---

<div class="post-metadata">

**Author:** ![selva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/13edae/32.png) [@selva](https://www.ruby-forum.com/u/selva)\
**Post date:** [April 22, 2008, 1:22pm UTC](https://www.ruby-forum.com/t/eager-loading-or-lazy-loading/133783/3 "2008-04-22T13:22:46Z")

</div>

Thanks…I got it…

Frederick C. wrote:

> On 3 Apr 2008, at 14:57, Selvaraj S. wrote:
> 
> > Hai…
> > 
> > Whether ruby on rails is eager loading or lazy loading???
> > 
> > I think while using association in model its getting eager loading…
> 
> Assuming I do bob = Person.find(124) and then do something with  
> bob.orders, then the association is lazy loaded, it’s not loaded until  
> you touch it. You can also eager load: bob = Person.find(124, :include  
> =\> ‘orders’)
> 
> Fred

---

<div class="post-metadata">

**Author:** ![selva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/13edae/32.png) [@selva](https://www.ruby-forum.com/u/selva)\
**Post date:** [April 22, 2008, 3:03pm UTC](https://www.ruby-forum.com/t/eager-loading-or-lazy-loading/133783/4 "2008-04-22T15:03:06Z")

</div>

> Frederick C. wrote:

> > Assuming I do bob = Person.find(124) and then do something with  
> > bob.orders, then the association is lazy loaded, it’s not loaded until  
> > you touch it. You can also eager load: bob = Person.find(124, :include  
> > =\> ‘orders’)

How do you use :include to add a column that must appear in the  
:conditions,  
_without_ eagerloading everything in that column’s table?

When we were tuning and we hit that one, we had to drop :include back to  
an  
old-fashioned :joins, which is really irritating!

A :lazy =\> true directive would have fixed it…

–  
Phlip

---

<div class="post-metadata">

**Author:** ![selva](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/13edae/32.png) [@selva](https://www.ruby-forum.com/u/selva)\
**Post date:** [April 22, 2008, 3:32pm UTC](https://www.ruby-forum.com/t/eager-loading-or-lazy-loading/133783/5 "2008-04-22T15:32:01Z")

</div>

On 22 Apr 2008, at 14:01, Phlip wrote:

> How do you use :include to add a column that must appear in  
> the :conditions,  
> _without_ eagerloading everything in that column’s table?
> 
> When we were tuning and we hit that one, we had to drop :include  
> back to an  
> old-fashioned :joins, which is really irritating!

You can do that with joins these days (:joins =\> :orders)

Fred
