Model class as join table

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?

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.


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!