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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Matthew Rudy Jacobs
2008-May-03 00:08 UTC
Re: Work out what are the included associations?
Alex Moore 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 RailsMatthew: Works perfectly in rails 2.0, thankyou for the solution! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
i think you can do person.address.loaded? On May 2, 5:08 pm, Matthew Rudy Jacobs <rails-mailing-l...@andreas- s.net> wrote:> Alex Moore 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 > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Matthew Rudy Jacobs
2008-May-05 01:41 UTC
Re: Work out what are the included associations?
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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---