If I have the following (abridged) setup: class User < ActiveRecord::Base has_many :groups, :through => :memberships end class Group < ActiveRecord::Base has_many :users, :through => :memberships end and my memberships table has: user_id group_id joined_date Let''s say I''ve got: @group.users.each do |user| . end How do I access their joined_date attribute? It''s gotta be easy but I can''t seem to figure it out. Do I need to write extra model code to pull out a single membership record? Thanks, Chad --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
sorry for the spaces sent html on accident --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Oct-21 20:27 UTC
Re: accessing :through table attributes
Hi -- On Sat, 21 Oct 2006, Chad Arimura wrote:> If I have the following (abridged) setup: > > class User < ActiveRecord::Base > has_many :groups, :through => :memberships > end > > class Group < ActiveRecord::Base > has_many :users, :through => :memberships > end > > and my memberships table has: > > user_id > group_id > joined_date > > Let''s say I''ve got: @group.users.each do |user| . end > > How do I access their joined_date attribute? It''s gotta be easy but > I can''t seem to figure it out. Do I need to write extra model code > to pull out a single membership record?The membership model exists to serve as the meeting place for all the info -- so go directly for the memberships: @group.memberships.each do |m| # do stuff with m.user and m.joined_date end David -- David A. Black | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB''s Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ah... that''s perfect. I was trying to pull a list of members and get the membership info from that as opposed to pulling memberships and getting member info. Thanks! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---