Accessing :through table attributes

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

sorry for the spaces sent html on accident

Hi –

On Sat, 21 Oct 2006, Chad A. wrote:

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 | [email protected]
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

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!