# Eager loading with has\_many :through

**URL:** <https://www.ruby-forum.com/t/eager-loading-with-has-many-through/161294>\
**Category:** Rails\
**Created:** [February 25, 2009, 5:47am UTC](https://www.ruby-forum.com/t/eager-loading-with-has-many-through/161294 "2009-02-25T05:47:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shenry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shenry](https://www.ruby-forum.com/u/shenry)\
**Post date:** [February 25, 2009, 5:47am UTC](https://www.ruby-forum.com/t/eager-loading-with-has-many-through/161294/1 "2009-02-25T05:47:24Z")

</div>

I’ve got the following associations:

class Photo \< AR::Base  
has\_many :slides  
has\_many :albums, :through =\> :slides

# this has an attachment\_fu attachment which creates a thumbnail

image for each uploaded photo.  
end

class Album \< AR::Base  
has\_many :slides  
has\_many :photos, :through =\> :slides  
end

class Slide \< AR::Base  
belongs\_to :photo  
belongs\_to :album  
end

In my Photos#index action I render all thumbnails and list the albums  
that the photos belong to. I’m using attachment\_fu, the parent image  
is the one with the slide and album association, the thumbnail image  
does not have that association.

The index action is hitting the database to load all albums that  
belong to a photo, how can I cut that down to one query for eager  
loading?

Currently I’m doing:

Photo.find(:all, :include =\> :parent, :conditions [‘thumbnail = ?’,  
‘small’])

but I can’t figure out how to eager-load the albums, through slides,  
based on the parent photo (since the thumbnail doesn’t have any  
associated slides or albums.)

Thanks for any help,

Stu

---

<div class="post-metadata">

**Author:** ![shenry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shenry](https://www.ruby-forum.com/u/shenry)\
**Post date:** [February 25, 2009, 9:03am UTC](https://www.ruby-forum.com/t/eager-loading-with-has-many-through/161294/2 "2009-02-25T09:03:12Z")

</div>

On Feb 25, 4:46 am, shenry [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Currently I’m doing:
> 
> Photo.find(:all, :include =\> :parent, :conditions [‘thumbnail = ?’,  
> ‘small’])
> 
> but I can’t figure out how to eager-load the albums, through slides,  
> based on the parent photo (since the thumbnail doesn’t have any  
> associated slides or albums.)

You can nest includes, eg Photo.find :all, :include =\> {:parent =\>  
[:some\_association\_on\_parent, :another\_association\_on\_parent]}

Fred

---

<div class="post-metadata">

**Author:** ![shenry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@shenry](https://www.ruby-forum.com/u/shenry)\
**Post date:** [February 25, 2009, 4:54pm UTC](https://www.ruby-forum.com/t/eager-loading-with-has-many-through/161294/3 "2009-02-25T16:54:28Z")

</div>

Sweet, thanks Fred.

On Feb 25, 12:02 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
