Hi This is difficult (for me, at least ;) ) I have a model named Measureunit and another named Vendor Each vendor has a measureunit for the currency it use, and the association is currency In vendor.rb I have belongs_to :currency, :class_name => "Measureunit" , :foreign_key => "currency_id" How can I recover the class name from the association name (eg. I have the string ''Currency'' and I look for a method that return ''Measureunit'') I found class_name() and klass() but I can''t figure the syntax... Tnx! -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/fSQcETbwG54J. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 15 November 2011 18:27, PandaR1 <fabio.montarsolo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi > This is difficult (for me, at least ;) ) > I have a model named Measureunit and another named Vendor > Each vendor has a measureunit for the currency it use, and the association > is currency > In vendor.rb I have > belongs_to :currency, :class_name => "Measureunit" , :foreign_key => > "currency_id" > > How can I recover the class name from the association name (eg. I have the > string ''Currency'' and I look for > a method that return ''Measureunit'')I don''t understand what you mean by ''I have the string "Currency"'' but if you have a vendor object then you can say vendor.currency.class.name Also I do not understand why you would want to do this so I have probably misunderstood the question. Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Nov 15, 12:27 pm, PandaR1 <fabio.montars...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi > > This is difficult (for me, at least ;) ) > > I have a model named Measureunit and another named Vendor > > Each vendor has a measureunit for the currency it use, and the association > is currency > > In vendor.rb I have > belongs_to :currency, :class_name => "Measureunit" , :foreign_key => > "currency_id" > > How can I recover the class name from the association name (eg. I have the > string ''Currency'' and I look for > a method that return ''Measureunit'') > > I found class_name() and klass() but I can''t figure the syntax...I believe what you''re saying is that you have the Vendor class, and the name of the association as a string, ''Currency''. You want to look at The ActiveRecord::Reflection documentation, specifically reflect_on_association (http://apidock.com/rails/ActiveRecord/ Reflection/ClassMethods/reflect_on_association). That gives you back an AssociationReflection, which has a bunch of information for you: http://apidock.com/rails/ActiveRecord/Reflection/AssociationReflection. Specifically again, you likely want klass on that, which should return the class of the association; do be aware that its a little bit more complex for polymorphic relationships if that''s necessary to support. So in summary what you likely want is something like s = "Currency" association_name = s.underscore # Or whatever transformation takes your string to your association name reflection = Vendor.reflect_on_association(association_name) # Returns the AssociationReflection object reflection.klass # Returns the class object, in this case Measureunit HTH, \Peter -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.