# Work out what are the included associations?

**URL:** <https://www.ruby-forum.com/t/work-out-what-are-the-included-associations/136526>\
**Category:** Rails\
**Created:** [May 3, 2008, 1:30am UTC](https://www.ruby-forum.com/t/work-out-what-are-the-included-associations/136526 "2008-05-03T01:30:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexmic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@alexmic](https://www.ruby-forum.com/u/alexmic)\
**Post date:** [May 3, 2008, 1:30am UTC](https://www.ruby-forum.com/t/work-out-what-are-the-included-associations/136526/1 "2008-05-03T01:30:47Z")

</div>

I have a person model:

Person  
has\_many :phone\_numbers  
has\_one :address  
has\_many :email\_addresses

When I do the query:  
people = Person.find(52, :include =\> [:address], :limit =\> 10)

How can I work out which associations we’re included.

i.e. in this case:  
which\_relationships\_were\_included(people) #=\> [‘address’]

The reason I want to do this is so that I can automatically marshall  
data in a specific format into javascript.

---

<div class="post-metadata">

**Author:** ![alexmic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@alexmic](https://www.ruby-forum.com/u/alexmic)\
**Post date:** [May 3, 2008, 2:08am UTC](https://www.ruby-forum.com/t/work-out-what-are-the-included-associations/136526/2 "2008-05-03T02:08:52Z")

</div>

Alex M. wrote:

> I have a person model:
> 
> Person  
> has\_many :phone\_numbers  
> has\_one :address  
> has\_many :email\_addresses
> 
> When I do the query:  
> people = Person.find(52, :include =\> [:address], :limit =\> 10)
> 
> How can I work out which associations we’re included.
> 
> i.e. in this case:  
> which\_relationships\_were\_included(people) #=\> [‘address’]
> 
> The reason I want to do this is so that I can automatically marshall  
> data in a specific format into javascript.

I think the best way is to look at what happens when you load an  
association.

person = Person.find(:first)  
person.instance\_variables == ["@attributes"]  
person.address  
person.instance\_variables == ["@address", “@attributes”]

person = Person.find(:first, :include =\> :address)  
person.instance\_variables == ["@address", “@attributes”]

I know that worked with Rails 1.1.6  
let me know if it doesnt in Rails 2

---

<div class="post-metadata">

**Author:** ![alexmic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@alexmic](https://www.ruby-forum.com/u/alexmic)\
**Post date:** [May 3, 2008, 2:14am UTC](https://www.ruby-forum.com/t/work-out-what-are-the-included-associations/136526/3 "2008-05-03T02:14:55Z")

</div>

Matthew R. Jacobs wrote:

> I think the best way is to look at what happens when you load an  
> association.
> 
> person = Person.find(:first)  
> person.instance\_variables == ["@attributes"]  
> person.address  
> person.instance\_variables == ["@address", “@attributes”]
> 
> person = Person.find(:first, :include =\> :address)  
> person.instance\_variables == ["@address", “@attributes”]
> 
> I know that worked with Rails 1.1.6  
> let me know if it doesnt in Rails

Matthew: Works perfectly in rails 2.0, thankyou for the solution!

---

<div class="post-metadata">

**Author:** ![alexmic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@alexmic](https://www.ruby-forum.com/u/alexmic)\
**Post date:** [May 3, 2008, 5:22am UTC](https://www.ruby-forum.com/t/work-out-what-are-the-included-associations/136526/4 "2008-05-03T05:22:40Z")

</div>

I spoke to soon …

So I can get a list of all the defined associations using:

assoc = model\_instance.class.reflections.values.map {|r| ‘@’ +  
r.name.to\_s} & model\_instance.instance\_variables

assoc.map {|a| a[1…-1]}.map {|a|  
model\_instance.class.reflections[a.to\_sym].class\_name}

But the issue is when i have  
people = Person.find(:all, :include =\> [:address,:suburb], :limit =\> 10)  
give\_me\_a\_list\_of\_all\_the\_included\_attributes\_including\_those\_in\_relationships(people)

I can’t simply inspect people[0] and look at those relationships,  
because if people[0] doesn’t have an address defined and people[1] does  
the left join means that I won’t see the suburb defined.

I thought an easy solution would be to hack up ActiveRecord::Base.find  
so that if an :include is passed, it stores that in each and every  
instance of the model. That way I can just inspect the hash and easily  
work out what was included.

The only other option I can think, is having my method  
give\_me\_a\_list\_of\_all\_the\_included\_attributes\_including\_those\_in\_relationships  
take another parameter which specifies the includes which in the long  
run is not as easy for the end user.

---

<div class="post-metadata">

**Author:** ![alexmic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@alexmic](https://www.ruby-forum.com/u/alexmic)\
**Post date:** [May 3, 2008, 8:14pm UTC](https://www.ruby-forum.com/t/work-out-what-are-the-included-associations/136526/5 "2008-05-03T20:14:55Z")

</div>

i think you can do person.address.loaded?

On May 2, 5:08 pm, Matthew R. Jacobs \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![alexmic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@alexmic](https://www.ruby-forum.com/u/alexmic)\
**Post date:** [May 5, 2008, 10:27am UTC](https://www.ruby-forum.com/t/work-out-what-are-the-included-associations/136526/6 "2008-05-05T10:27:45Z")

</div>

Matthew R. Jacobs wrote:

> colin wrote:
> 
> > i think you can do person.address.loaded?
> > 
> > On May 2, 5:08ï¿½pm, Matthew R. Jacobs \<rails-mailing-l…@andreas-
> 
> good spot Colin!

Cheers Guys!

---

<div class="post-metadata">

**Author:** ![alexmic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/977dab/32.png) [@alexmic](https://www.ruby-forum.com/u/alexmic)\
**Post date:** [May 5, 2008, 3:41am UTC](https://www.ruby-forum.com/t/work-out-what-are-the-included-associations/136526/7 "2008-05-05T03:41:54Z")

</div>

colin wrote:

> i think you can do person.address.loaded?
> 
> On May 2, 5:08ï¿½pm, Matthew R. Jacobs \<rails-mailing-l…@andreas-

good spot Colin!
