如何在 Rails 中加入多角色、多组织表 [英] How to join mutli-role, multi organisation tables in Rails

查看:20
本文介绍了如何在 Rails 中加入多角色、多组织表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 Rails 设计找到一个对我来说并不那么明显的解决方案.一个非常擅长这个东西的朋友给了我他的看法,但我想知道是否有一个 rails 模式 - 我缺少的知识是 rails 如何创建关系......

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…

我有这样的问题空间.用户可以在多个组织中扮演多个角色.因此,例如,用户可以是组织 1 的标准用户"和高级用户",但可以是组织 2 的管理员".

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.

我正在使用 Devise 和 CanCan.我有一个 Users 表、一个 Roles 和一个 Organizations 表以及一个 roles_users 表来管理这种多对多关系.然后我有一个 user_organisations 表,它存储用户和组织之间的 M2M.这一切正常.当我这样做时;

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.

缺少的一点是角色.我有一个新模型(使用通过赋值),roles_user_orgs,它是 user_org 表和角色之间的链接,并通过使用 user_org 的主键和角色 ID 为用户存储角色.但它永远不会被填充.我不知道这是因为我没有正确编写插入来填充它,还是因为我的关系不正确 - 或者 - 因为设计完全错误并且无法在导轨中工作.更简单的模型是在user_roles表中使用一个org_id,但是我不知道如何填充这个……

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……

这是我的联想...

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

推荐答案

我认为你在这里把事情复杂化了.在我看来,您需要一个名为 memberships 的表,它应该充当用户、组织和角色之间的连接表

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))

这篇关于如何在 Rails 中加入多角色、多组织表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆