Forum: Ruby on Rails Model class as join table

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
B609dc8cf73079db56b3979205e37753?d=identicon&s=25 Rod (Guest)
on 2007-04-03 17:25
(Received via mailing list)
I'm a Ruby noob, and I'm pretty convinced there's an elegant way to
implement what I'm trying to do.  I thought I'd ask if there's any
idioms I could use.

Two classes: "team" and "member" joined in a team_members table.  In
addition to the IDs, the team_members group has a "permissions"
column.  The permission can take three values: creator, participant or

The Member model has three collections: created_teams, ,
participating_teams, and observed_teams.  I use the following
construction (this is only the observers collection, but it's the same
for the other two.)

has_many  :member_teams
has_many  :observed_teams,
        :through => :member_teams,
        :source => :teams,
        :conditions => "member_teams.permissions = '#{Observer}'

Works like a charm and I love it.  Here's my question.

I'd like to set the permissions field on the member_teams table when I
assign it to a collection.  For example,

t =
member.participating_teams << t

That does insert a record in the join table, but I have to access the
join record manually and set the permission, otherwise it's null.  I'd
like it to be set to "participant" in the instance above, or
"observer" if added to the observed_teams collection, etc.

Is there a Ruby Way?
B609dc8cf73079db56b3979205e37753?d=identicon&s=25 Rod (Guest)
on 2007-04-03 19:11
(Received via mailing list)
In case anybody hits this in a search, I accomplished this via the
"after_save" method of the model.

def after_save
user_resources.each do |ur|
      if !ur.user_type
        if observers.detect {|user| == ur.user_id }
          ur.user_type = User::Observer
        elsif participants.detect {|user| == ur.user_id }
          ur.user_type = User::Participant
        elsif creators.detect {|user| == ur.user_id }
          ur.user_type = User::Creator
B609dc8cf73079db56b3979205e37753?d=identicon&s=25 Rod (Guest)
on 2007-04-03 19:14
(Received via mailing list)
Whups, accidental post.  Darn touchpads.

Anyhow, I accomplished it as below (some class names have changed, I'm
sure you can get it though) but it seems tres ugly.  If there's a
better way I'm all ears.

This topic is locked and can not be replied to.