Karthik Muthu
Karthik Muthu

Reputation: 135

Rails associtating three models

I have the following models in my rails application

class User < ApplicationRecord 
end
class Group < ApplicationRecord 
end
class Role < ApplicationRecord 
end

Every Group has_many User with different Role. For example Group-1 have User - A with Role - Admin and Group-1 have User - B with Role - Member and Group-2 have User - A with Role - Member. Like wise each Group have multiple Users and every User have multiple Role.

Please guide What kind of association should I use?

Upvotes: 4

Views: 601

Answers (3)

Naman Wadhwa
Naman Wadhwa

Reputation: 148

You can also create a Tri-related table having a record for every membership of a user and saving that user's role as well.

class Trirelation
  belongs_to :group
  belongs_to :user
  belongs_to :role
end

class User
  has_many :trirelations
  has_many :groups, through: :trirelations
end

class Group
  has_many :trirelations
  has_many :users, through: :trirelations
end

class Role
  has_many :trirelations
end

In case you need to query whether a given user is an admin of a group,

@role = Trirelation.find_by(user_id: 1, group_id: 21).role.name

Upvotes: 1

webster
webster

Reputation: 4012

Assuming that a user belongs to only one group and has only one role.

Associations in model:

class Group < ApplicationRecord 
  has_many :users
end

class User < ApplicationRecord 
  belongs_to :group
  belongs_to :role
end

class Role < ApplicationRecord 
  has_many :users
end

Columns that should be added to the models:

user:

group_id:integer:index
role_id:integer:index

EDIT 1:

Updating the associations based on the comments.

class Group < ApplicationRecord 
  has_many :users
  has_many :roles
end

class User < ApplicationRecord 
  belongs_to :group
end

class Role < ApplicationRecord 
  belongs_to :group
end

Columns that should be added to the models:

user:

group_id:integer:index

role:

group_id:integer:index

To find the group and roles of a user you can use:

user = User.last
user.group # Returns group of the user
user.group.roles # Returns roles of the user

Upvotes: 1

Narasimha Reddy - Geeker
Narasimha Reddy - Geeker

Reputation: 3860

Incase, If user belongs to many groups and role belongs to many users, I hope the following associations works fine...

class Group < ApplicationRecord 
  has_many :group_users
  has_many :users, through: :group_users

  has_many :group_user_roles
  has_many :roles, through: :group_user_roles
end

class User < ApplicationRecord 
  has_many :group_users
  has_many :groups, through: :group_users

  has_many :group_user_roles
  has_many :roles, through: :group_user_roles
end

class GroupUser < ApplicationRecord
    belongs_to :group
    belongs_to :user
end

class Role < ApplicationRecord
end

class GroupUserRole < ApplicationRecord
  belongs_to :user
  belongs_to :group
  belongs_to :role
end

You need to create one more table(GroupUserRole) to associate multiple users with multiple groups for multiple roles.

Upvotes: 1

Related Questions