# Dynamically add includes to an association?

**URL:** <https://www.ruby-forum.com/t/dynamically-add-includes-to-an-association/158349>\
**Category:** Rails\
**Created:** [January 22, 2009, 11:38am UTC](https://www.ruby-forum.com/t/dynamically-add-includes-to-an-association/158349 "2009-01-22T11:38:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [January 22, 2009, 11:38am UTC](https://www.ruby-forum.com/t/dynamically-add-includes-to-an-association/158349/1 "2009-01-22T11:38:05Z")

</div>

I have an association user.lessons. Sometimes i just want the lessons,  
other times i want the lessons to eager-load one of their own  
associations, ‘assets’. Theres a couple of ways i can see to deal with  
this and i’m not super happy with any of them.

approach 1) Make two associations:  
has\_many :lessons  
has\_many :lessons\_with\_assets, :class\_name =\> “Lesson”, :include =\>  
[:assets]

#then, if i want the assets, call  
@user.lessons\_with\_assets

This is actually my favorite but it still feels a bit clumsy

approach 2) Don’t use the association if i want to eager load  
#just do a regular find  
Lesson.find\_all\_by\_user\_id(@user.id, :include =\> [:assets])

Is there another way? This example is simple but i have other  
associations that go through join tables and are polymorphic (eg using  
the has\_favorite plugin) that are a bit more arsey to use.

thanks  
max

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [January 22, 2009, 12:09pm UTC](https://www.ruby-forum.com/t/dynamically-add-includes-to-an-association/158349/2 "2009-01-22T12:09:18Z")

</div>

On Thu, Jan 22, 2009 at 12:38 PM, Max W. \<  
removed\_email\_address@domain.invalid\> wrote:

> associations that go through join tables and are polymorphic (eg using  
> the has\_favorite plugin) that are a bit more arsey to use.
> 
> ## thanks max
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

Max

There’s another way, named\_scopes.

in the Lesson model, add  
named\_scope :with\_assets, :include =\> :assets

then you can call  
lessons  
or  
lessons.with\_assets

–  
Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

“I have never let my schooling interfere with my education” - Mark Twain

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [January 22, 2009, 12:37pm UTC](https://www.ruby-forum.com/t/dynamically-add-includes-to-an-association/158349/3 "2009-01-22T12:37:17Z")

</div>

aha…so, if i have lots of different associations (in user and other  
classes) that return a collection of lessons i can just add  
“.with\_assets” to the end? eg

user.usb\_lessons.with\_assets

user.lessons.with\_assets

resource.recent\_lessons.with\_assets

etc  
?

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [January 22, 2009, 12:58pm UTC](https://www.ruby-forum.com/t/dynamically-add-includes-to-an-association/158349/4 "2009-01-22T12:58:23Z")

</div>

After a bit of experimentation i confirmed for myself that that is  
indeed the case 🙂

thanks a lot Andrew, this is great. I’d seen named scopes before but  
never really used them much and never realised their potential for  
managing includes.

One thing i noticed which is interesting…lessons are joined to assets  
through a join table, ‘elements’.

if i use the with\_assets scope for an association that goes through a  
join table, then rails does a further join to the assets table, like  
you’d expect with an include, so we have this situation:

user JOIN user\_usb\_lessons JOIN lessons JOIN elements JOIN assets

If, however, it’s a simple association (such as user.lessons, which just  
uses Lesson’s user\_id field) then rails splits it off into three sql  
calls: the first to load the lessons, the second to load the elements  
(join table records) and the third to load the assets.

I guess that three seperate simple calls were seen as preferable to a  
single JOIN?

Anyway, thanks again!  
max

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [January 22, 2009, 1:04pm UTC](https://www.ruby-forum.com/t/dynamically-add-includes-to-an-association/158349/5 "2009-01-22T13:04:47Z")

</div>

On Thu, Jan 22, 2009 at 1:58 PM, Max W. \<  
removed\_email\_address@domain.invalid\> wrote:

> I guess that three seperate simple calls were seen as preferable to a  
> single JOIN?
> 
> ## Anyway, thanks again! max
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

Yes, on more complicated joins, where Rails thinks it will be better, it  
does split the query into multiple queries.

–  
Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

“I have never let my schooling interfere with my education” - Mark Twain

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [January 22, 2009, 12:58pm UTC](https://www.ruby-forum.com/t/dynamically-add-includes-to-an-association/158349/6 "2009-01-22T12:58:48Z")

</div>

On 22 Jan 2009, at 11:08, Andrew T. wrote:

> then you can call  
> lessons  
> or  
> lessons.with\_assets

See also  
[http://ryandaigle.com/articles/2008/8/20/named-scope-it-s-not-just-for-conditions-ya-know](http://ryandaigle.com/articles/2008/8/20/named-scope-it-s-not-just-for-conditions-ya-know)

Fred
