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

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

问题描述

我试图找到一个解决方案,一个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.

m使用Devise和CanCan。我有一个Users表,Roles and 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存储用户的角色。但它从来没有人口。我不知道这是否是因为我没有正确地写入插入来填充它,或者因为我的关系不正确 - 或者 - 因为设计是完全错误的,不能在rails中工作。 simper模型是在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)

要查询组织,您可以:

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

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

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