How to join mutli-role, multi organisation tables in Rails

安稳与你 提交于 2019-12-03 21:56:48

问题


I'm trying to find a solution to a rails design that isn't all that obvious to me. A friend who is very good with this stuff has given me his take on it, but I wondered if there is a rails pattern - the knowledge I'm missing is how rails creates the relationship…

I have a problem space like this. Users can perform more than one role at more than one organisation. So for example, a user can be both a "Standard User" and an "Power User" at Organisation 1, but an "Admin" at Organisation 2.

I'm using Devise and CanCan. I have a Users table, Roles and an Organisations table and a roles_users table to manage that many-to-many relation. Then I have a user_organisations table, which stores the M2M between a user and the organisations. This all works okay. When I do this;

user = User.new({ :email => 'admin@example.com', 
:password => 'password', 
:password_confirmation => 'password', 
:firstname => 'TestFirstName',
:surname => 'TestSurName'})

org1 = Org.new({:fullname => 'Test Org 1'})
org1.save

org2 = Org.new({:fullname => 'Test Org 2'})
org2.save

user.org << Org.first
user.org << Org.last

user.roles << Role.where('name'=>'administrator').first
user.roles << Role.where('name'=>'PowerUser').first

I get (as you would expect) two orgs, and a user registered at both.

The missing bit is the roles. I have a new model (using through assignment), roles_user_orgs, which is meant to be the link between the user_org table and the roles, and store the role for the user by using the primary key of the user_org and the roles id. But it is never populated. I don't know if this is because I am not writing the insert correctly to populate it, or because my relations aren't correct - or - because the design is plain wrong and won't work in rails. The simper model is to use an org_id in the user_roles table, but I don't know how to populate this……

Here's my associations...

class Role

has_many :user_roles, :dependent => :destroy
has_many :users, :through => :user_roles, :uniq => true

has_many :role_user_orgs, :dependent => :destroy
has_many :user_orgs, :through => :role_user_orgs


class Org
has_many :user_orgs
has_many :users, :through => :user_orgs

class UserOrg
belongs_to :org
belongs_to :user

has_many :role_user_orgs, :dependent => :destroy
has_many :roles, :through => :role_user_orgs

class UserRole
belongs_to :User
belongs_to :role

class User
has_many :user_roles
has_many :roles, :through => :user_roles
has_many :user_orgs
has_many :orgs, :through => :user_orgs


class RoleUserOrg
belongs_to :role
belongs_to :user_orgs

回答1:


I think you're overcomplicating things here. The way I see it, you need a single table called memberships that should act as a join table between User, Org and Role

class Membership
  belongs_to :user
  belongs_to :org
  belongs_to :role


class User
  has_many :memberships
  has_many :orgs, through: :memberships
  has_many :roles, through: :memberships

To create a new role for a user within an organization, just do:

org = Org.create({:fullname => 'Test Org 1'})
role = Role.where('name'=>'administrator').first
membership = user.memberships.create(org_id: org.id, role_id: role.id)

To query for the roles within an organization, you can do:

org = Org.where(name: "Test Org").first
my_roles = Role.find(user.memberships.where(org_id: org.id).map(&:role_id))


来源:https://stackoverflow.com/questions/16771503/how-to-join-mutli-role-multi-organisation-tables-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!