dino d.
2010-May-11 15:05 UTC
can access the through attributes from the many with has_many through?
hi - if i have A has_many B A has_many C through B Is there any way to retrieve the B attributes once I have a C object? For example: class User < ... has_many :friend_links has_many :friends, :through => :friend_links, :class => User ... friends = user.friends for f in friends link = f.friend_link #i want to get the through object because i store some metrics here is this possible? i realize i can just ignore the through and just grab the friend_links directly, but i have various types of friends, some are borne of friend_links, others have a different genesis, and i''d like to just treat them in one fell swoop, and if one of them happens to be born of a through, i''ll do one thing, but otherwise, i''ll do something else. thanks for any help, dino -- 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.
Colin Law
2010-May-11 15:18 UTC
Re: can access the through attributes from the many with has_many through?
On 11 May 2010 16:05, dino d. <dinodorroco-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:> hi - > > if i have > > A has_many B > A has_many C through B > > Is there any way to retrieve the B attributes once I have a C object?Assuming that you also have B has_many C C belongs_to B then if you have a C in @c you can say @c.b Colin> > For example: > > class User < ... > > has_many :friend_links > has_many :friends, :through => :friend_links, :class => User > ... > > friends = user.friends > for f in friends > link = f.friend_link #i want to get the through object because i > store some metrics here > > is this possible? i realize i can just ignore the through and just > grab the friend_links directly, but i have various types of friends, > some are borne of friend_links, others have a different genesis, and > i''d like to just treat them in one fell swoop, and if one of them > happens to be born of a through, i''ll do one thing, but otherwise, > i''ll do something else. > > thanks for any help, > dino > > -- > 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. > >-- 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.