# Work out what are the included associations?

**URL:** <https://discuss.rubyonrails.org/t/work-out-what-are-the-included-associations/28882>\
**Category:** rubyonrails-talk\
**Created:** [May 2, 2008, 11:30pm UTC](https://discuss.rubyonrails.org/t/work-out-what-are-the-included-associations/28882 "2008-05-02T23:30:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [May 2, 2008, 11:30pm UTC](https://discuss.rubyonrails.org/t/work-out-what-are-the-included-associations/28882/1 "2008-05-02T23:30:47Z")

</div>

I have a person model:

Person &nbsp;&nbsp;has\_many :phone\_numbers &nbsp;&nbsp;has\_one :address &nbsp;&nbsp;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:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [May 3, 2008, 12:08am UTC](https://discuss.rubyonrails.org/t/work-out-what-are-the-included-associations/28882/2 "2008-05-03T00:08:52Z")

</div>

Alex Moore wrote:

> I have a person model:
> 
> Person &nbsp;&nbsp;has\_many :phone\_numbers &nbsp;&nbsp;has\_one :address &nbsp;&nbsp;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:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [May 3, 2008, 12:14am UTC](https://discuss.rubyonrails.org/t/work-out-what-are-the-included-associations/28882/3 "2008-05-03T00:14:55Z")

</div>

Matthew Rudy 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:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [May 3, 2008, 3:22am UTC](https://discuss.rubyonrails.org/t/work-out-what-are-the-included-associations/28882/4 "2008-05-03T03: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:** ![colin1](https://avatars.discourse-cdn.com/v4/letter/c/ecb155/32.png) [@colin1](https://discuss.rubyonrails.org/u/colin1)\
**Post date:** [May 3, 2008, 5:58pm UTC](https://discuss.rubyonrails.org/t/work-out-what-are-the-included-associations/28882/5 "2008-05-03T17:58:48Z")

</div>

i think you can do person.address.loaded?

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [May 5, 2008, 1:41am UTC](https://discuss.rubyonrails.org/t/work-out-what-are-the-included-associations/28882/6 "2008-05-05T01:41:54Z")

</div>

colin wrote:

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

good spot Colin!

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [May 5, 2008, 8:27am UTC](https://discuss.rubyonrails.org/t/work-out-what-are-the-included-associations/28882/7 "2008-05-05T08:27:45Z")

</div>

Matthew Rudy Jacobs wrote:

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

Cheers Guys!
