Work out what are the included associations?

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.

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

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!

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.

i think you can do person.address.loaded?

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

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!

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!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs