How can I dynamically find associations of a model? eg. class TravelService < ActiveRecord::Base belongs_to :main_service belongs_to :travel_grade belongs_to :travel_type end tempClass = "TravelService".constantize Now, I want to find associations of tempClass. Expected value: tempClass = [MainService, TravelGrade, TravelType] Thanks in advance for any help. Thushan --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi Thushan,> How can I dynamically find associations of a model? > > eg. > class TravelService < ActiveRecord::Base > belongs_to :main_service > belongs_to :travel_grade > belongs_to :travel_type > end > > tempClass = "TravelService".constantize > > Now, I want to find associations of tempClass. > > Expected value: > tempClass = [MainService, TravelGrade, TravelType]use : tempClass.reflect_on_all_associations or tempClass.reflect_on_all_associations(:belongs_to) ... if you only want the belongs_to associations. To collect the name of belongs_to associations, you can then write : tempClass.reflect_on_all_associations(:belongs_to).map(&:name) it will not return an array of constants, since an association name can just be a name, not necesseraly related to a class name. -- Jean-François. -- À la renverse. --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com 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 -~----------~----~----~----~------~----~------~--~---
Wow! Thanks Jean! On Feb 9, 3:05 pm, "Jean-François" <jf.w...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi Thushan, > > > How can I dynamically find associations of a model? > > > eg. > > class TravelService < ActiveRecord::Base > > belongs_to :main_service > > belongs_to :travel_grade > > belongs_to :travel_type > > end > > > tempClass = "TravelService".constantize > > > Now, I want to find associations of tempClass. > > > Expected value: > > tempClass = [MainService, TravelGrade, TravelType] > > use : > > tempClass.reflect_on_all_associations > > or > > tempClass.reflect_on_all_associations(:belongs_to) > > ... if you only want the belongs_to associations. > > To collect the name of belongs_to associations, you can then > write : > > tempClass.reflect_on_all_associations(:belongs_to).map(&:name) > > it will not return an array of constants, since an association name > can just be a name, not necesseraly related to a class name. > > -- Jean-François. > > -- > À la renverse.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---