角色相关的协会 [英] Role-dependent associations

查看:109
本文介绍了角色相关的协会的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作我的第一个真正的Rails应用程序的时间跟踪的公司,我遇到了数据库/模型设计的关注。我的应用程序有一个用户模式,它们通过has_and_belongs_to_many协会联系在一起的榜样。

其中一个角色是经理。管理者可以有许多助手和助理可以有许多经理人(虽然通常只有一个)。这种关系是通过pssed在用户模型作为manager_assistant_relationship EX $ P $重新presented:

 的has_many:助理:通过=> :manager_assistant_relationships
的has_many:经理:通过=> :manager_assistant_relationships
 

助理可以有很多的变化(不同的模型重新presenting轮班)和管理人员可以有许多客户端(另一种模式,将被收取助理的工作)。而更多的协会来了。

据我可以告诉唯一的解决办法就是把所有这些关系,在用户模式,但有什么感觉错了这一点,并在用户模式可以让臃肿的角色增加和关联安装,这也似乎不直观,因为用户只要拥有许多助手,如果他是经理。我想了解潜在的陷阱,以这种方式和什么选择,我应该考虑的一些建议。

这将是子类的用户模型转换角色是个好主意?

解决方案

好消息,你是正确的做用的角色。

要回答你的问题,的没有的它不是继承的用户模式为你介绍个好主意。如果你做到了,它往往会导致棘手的自连接的关联,或STI(单表继承),或继承的类,而不是模块的组成或。最好使用的角色。

它可以帮助你去思考用户作为一个人,而不是一个角色。因此,用户类仅包含个人信息,如姓名,出生日期,身高,也许一个电子邮件地址,登录密码等,使从时间跟踪作用的个人信息区分开来。

要使用你的角色,你可以做这样的伪code模型关系:

 类管理
   的has_many:客户
 结束

 经理= Manager.find(ID)
 客户端= manager.clients.first
 

或者类似这样的伪code:

 类助理
   的has_many:workshifts
 结束

 助理= Assistant.find(ID)
 轮班= assistant.workshifts
 

其他好的理由来使用角色preFER是基于角色的访问控制(RBAC) 数据上下文互为作用(DCI),如果​​你想阅读关于这些了。<​​/ P>

I am working on my first real Rails app for timetracking in a company and I have run into database/model design concern. My app has a User model and a Role model which are linked together through a has_and_belongs_to_many association.

One of the roles is the manager. A manager can have many assistants and assistants can have many managers (although typically only one). This relationship is represented through a manager_assistant_relationship expressed in the User model as:

has_many :assistants, :through => :manager_assistant_relationships
has_many :managers, :through => :manager_assistant_relationships

Assistants can have many shifts (a different model representing a work shift) and managers can have many clients (another model that will be billed for the assistants' work). And more associations are coming.

As far as I can tell the only solution is to put all these relationships in the User model, but something feels wrong about this and the User model could get bloated as roles increases and associations mount and it also seems unintuitive, because a User only has many assistants if he is a manager. I would like some advice about potential pitfalls to this approach and what alternatives I should consider.

Would it be a good idea to subclass the User model into roles?

解决方案

Good news, you're doing it correctly with roles.

To answer your question, no it is not a good idea to subclass the User model for what you describe. If you did, it tends to lead to tricky self-joins for associations, or to STI (single table inheritance), or to inheritance or classes instead of composition of modules. Much better to use roles.

It may help you to think of the user as a person, not a role. So the User class contains just personal information, such as name, birthdate, height, and maybe an email address, sign in password, etc. Make the personal information separate from the time tracking role.

To use your roles, you can do model relationships like this psuedocode:

 class Manager
   has_many :clients
 end

 manager = Manager.find(id)
 client = manager.clients.first

Or like this pseudocode:

 class Assistant
   has_many :workshifts
 end

 assistant = Assistant.find(id)
 workshift = assistant.workshifts

Other good reasons to prefer using roles is for Role-Based Access Control (RBAC) and for Data Context Interation (DCI) if you want to read up on these.

这篇关于角色相关的协会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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