Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Many-to-many Users and groups, but groups have owners

I'm having trouble trying to understand/wrap my brain around this. I'm trying to create a relationship that allows this:

  • user can belong to many groups
  • group can have many users
  • a group has an owner which is a user
  • the group ownership can be transferable

I've got the many-to-many relationship set up, but I can't seem to understand how to set up the ownership functionality.

here is what i have so far in my models:

    class Group < ActiveRecord::Base
      has_and_belongs_to_many :users
      attr_accessible :name, :description, :isPublic, :tag_list, :owner
    end

    class User < ActiveRecord::Base
      has_and_belongs_to_many :groups
      attr_accessible :name, :description, :owner_id
    end

Any help would be greatly appreciated!!

like image 684
meatherly Avatar asked Mar 15 '13 21:03

meatherly


2 Answers

You can set it up a couple of ways:

1) Use a join model and place a flag on the join model that specifies that the group member is an owner.

class Group < ActiveRecord::Base
  has_many :memberships
  has_many :users, through: :memberships
  attr_accessible :name, :description, :isPublic, :tag_list, :owner
end

class Membership < ActiveRecord::Base
  belongs_to :group
  belongs_to :user

  #this table has a flag called owner and thus a method called owner?
end

class User < ActiveRecord::Base
  has_many :memberships
  has_many :groups, through: :memberships
  attr_accessible :name, :description, :owner_id
end

2) Keep your existing HABTM and add another join model for tracking ownership.

class Group < ActiveRecord::Base
  has_and_belongs_to_many :users
  has_many :group_ownerships
  has_many :owners, through: :group_owernships, class_name: "User"
  attr_accessible :name, :description, :isPublic, :tag_list, :owner
end

class GroupOwnership < ActiveRecord::Base
  belongs_to :group
  belongs_to :user
end

class User < ActiveRecord::Base
  has_and_belongs_to_many :groups
  has_many :group_ownerships
  has_many :owned_groups, through: :group_owernships, class_name: "Group"
  attr_accessible :name, :description, :owner_id
end
like image 98
Sean Hill Avatar answered Oct 23 '22 16:10

Sean Hill


I think that one solution could be to define a new belongs_to relationship from the group to the owner. So you would need to add a new user_id column in the groups table.

class Group < ActiveRecord::Base
  has_and_belongs_to_many :users
  belongs_to :owner, class_name: 'User', foreign_key: 'user_id'
  attr_accessible :name, :description, :isPublic, :tag_list, :owner
end

class User < ActiveRecord::Base
  has_and_belongs_to_many :groups
  has_many :owned_groups, class_name: 'Group', foreign_key: 'user_id'
  attr_accessible :name, :description, :owner_id
end

I think that's what you need. with a user, you can have all the groups he's a member of with user.groups, and you can have all the groups he owns user.owned_groups. With a group, you can have its owner : group.owner and all its members : group.users.

If you want to change the owner just do group.owner = new_user with new_user begin a User instance

Note that I quickly picked named for association and foreign keys, you could of course customize that.

like image 38
pjam Avatar answered Oct 23 '22 14:10

pjam