Resources, has_many :through and extra attributes on 'join model'


I found a small ‘problem’ to which I think I found a solution.
But the solution feels a bit hacky.
I would like some feedback on it.

Say I have Groups & Users.
A group has many users, but a user can also belong to many groups.
Also, the role a user has in a group should be known.

  1. class Group < ActiveRecord::Base
  2. has_many :memberships
  3. has_many :users, :through => :memberships
  4. end
  5. class User < ActiveRecord::Base
  6. has_many :memberships
  7. has_many :groups, :through => :memberships
  8. end
  9. class Membership < ActiveRecord::Base
  10. belongs_to :group
  11. belongs_to :user
  12. validates_uniqueness_of :user_id, :scope => :group_id
  13. this model has a field “role” which describes the role a

user has in the group
17. end

Now, this suffices for the database and the model, but in my routes
file I don’t want to expose memberships as resources (since they are
kind of artificial), so:

  1. map.resources :groups do |group|
  2. group.resources :users
  3. end
  4. map.resources :users do |user|
  5. user.resources :groups
  6. end

This works, I can now see which users are in a group(on /groups/2/
users), but there is no way to see what role they have.

My solution would be to give users a virtual attribute (role) which
should get filled in when they are acquired by a group.

something along the lines of

  1. class Group < ActiveRecord::Base
  2. has_many :memberships
  3. has_many :users, :through => :memberships
  4. def users_with_roles
  5. array = []
  6. memberships.each do |membership|
  7.   user = membership.user
  8.   user.role = membership.role
  9.   array << user
  10. end
  11. array
  12. end
  13. end

Now, I can use @group.users_with_roles in my userscontroller’s index
action and display the list of users, including their role in this

Of course this needs some changes for finding single users also, and
code to handle saving in case of adding users to a group.

But before I go that way I need to be assured this is the/a way to go.
Like I said, it feels very hacky, so I guess there should be a better
way to do this.

Any thoughts on this subject?
Like I said, I don’t want to expose memberships to the outside world,
just users & groups.

Thanks, Mathijs