So I'm having an interesting issue with my database and associations.

I have three models,  Users, Groups, and Roles.  They are connected via
a join table that has user_id, group_id, and role_id on the same table.
A user is in a group, with a role.  The same user can be in another
group with another (or the same) role.

So, the user model has a has_and_belongs_to_many :roles, and the role
model has a has_and_belongs_to_many :users, with the appropriate tags to
make it work.

With this, @user.roles will grab a list of all roles associated with
that user, and @role.users will grab all the users that have that role.
Duplicates are possible.

One interesting behavior, was that, if the user and the role model both
had a group_id column, then when @user.roles was called (and likewise
@role.users), it would grab the group_id from the join_table for each
role, instead of the group_id from users or roles.  If I take out the
group_id from users and roles, then it does not appear when I call
@user.roles

This behavior is desirable, because then it lets me do things like,

@user.roles[@user.roles.index{|x| x.group_id==1}].name

to get the name of the role that the user has in group 1.  Things like
that.

So my question is, how can I get that functionality (grabbing the
group_id when going through the join table) without the group_id columns
in the user and role tables?

I've tried declaring group_id as attr_accessor :group_id for both user
and role, but that didn't work.

The most important thing is being able to maintain the behavior, that I
can call @user.roles[0].group_id, and @roles.user[0].group_id, with
group_id being grabbed from the join table.  Any way to do that would
solve my problem.

Is there a way I can do it with definitions?  like def
self.Role.group_id or something like that?

-- 
Posted via http://www.ruby-forum.com/.

-- 
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@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to