# Eager Loading

**URL:** <https://www.ruby-forum.com/t/eager-loading/112658>\
**Category:** Rails\
**Created:** [August 21, 2007, 9:45pm UTC](https://www.ruby-forum.com/t/eager-loading/112658 "2007-08-21T21:45:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 21, 2007, 9:45pm UTC](https://www.ruby-forum.com/t/eager-loading/112658/1 "2007-08-21T21:45:46Z")

</div>

is there a way to always eagerly load an associated object so that you  
don’t have to put :include into every finder?

class Product \<ActiveRecord::Base  
belongs\_to :user #always load user when product is loaded  
end

Product.find(1).user.login

#1 query since user was loaded with the product, same as if i  
used :include but I don’t want to have to specify it everywhere.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 22, 2007, 6:15am UTC](https://www.ruby-forum.com/t/eager-loading/112658/2 "2007-08-22T06:15:31Z")

</div>

i’m not aware of any config to do this automatically… perhaps  
someone more experienced knows a way.

another way is to make a method in your model:

class Product \<ActiveRecord::Base  
belongs\_to :user

def self.find\_eager(id)  
self.find(id, :include =\> :user)  
end

end

note you’d have to do a little more work to get it to work with all  
options to find(), like :all, :conditions, :order etc…

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 22, 2007, 6:31am UTC](https://www.ruby-forum.com/t/eager-loading/112658/3 "2007-08-22T06:31:32Z")

</div>

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

> def self.find\_eager(id)  
> self.find(id, :include =\> :user)  
> end
> 
> end
> 
> note you’d have to do a little more work to get it to work with all  
> options to find(), like :all, :conditions, :order etc…

I would do it similarly.

I believe this would be a good fit for with\_scope

> **[ActiveRecord::Base](https://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001024)**
>
> Active Record Active Record objects don't specify their attributes directly, but rather infer them from the table definition with which they're linked.

class Product \<ActiveRecord::Base  
belongs\_to :user

def self.find\_eager(\*args)  
with\_scope( :find =\> { :include =\> [:user] } ) do  
find(\*args)  
end  
end

end

You could also alias\_method\_chain find on the Product class and wrap  
find in  
a with\_scope there instead. That way could be neater.

Cheers  
Daniel

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 22, 2007, 7:06am UTC](https://www.ruby-forum.com/t/eager-loading/112658/4 "2007-08-22T07:06:34Z")

</div>

On 8/22/07, Daniel N [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > note you’d have to do a little more work to get it to work with all
> 
> Cheers  
> Daniel

I thought I’d check out how to alias\_method\_chain for the Product Class  
to  
wrap the find method. This is untested mostly. YMMV

class Product \<ActiveRecord::Base  
belongs\_to :user

```
class << self
  def find_with_user_scope( *args )
    with_scope( :find => { :include => :user } ) do
       find( *args )
    end
  end
  alias_method_chain :find, :user_scope
end

```

end

This way whenever you call Product.find it will be scoped to include the  
user. If you don’t want to include the user then you can use

Product.find\_without\_user\_scope( \*args )

HTH  
Daniel
