在 Rails 中对具有不同属性的角色进行建模的最佳方法是什么?
我是 Rails 菜鸟,希望有人能帮助我解决这个问题。我有一个应用程序,它有一个用户模型,使用 Authlogic 进行身份验证,使用 CanCan 进行授权。共有三种角色:消费者、企业和管理员。用户可以拥有任意数量的这些角色。
然而,企业有其他属性,我需要对此进行建模,以便我可以添加每个角色都具有潜在不同属性的角色。直觉告诉我,我需要有一个单独的模型来表示每个角色的属性,即 BusinessRoleProfile、ConsumerRoleProfile 等,然后可能以编程方式混合一个模块,将适当的 has_one 引用添加到配置文件模型中。
这是处理单独属性的最佳方法吗?如果是这样,有人可以指导我如何根据用户的角色动态包含这些 mixins 吗?
I'm a Rails noob and am hoping someone can help me wrap my head around this issue. I have an app that has a single User model using Authlogic for authentication and CanCan for authorization. There are three roles: Consumer, Business, and Admin. A user can have any number of these roles.
Businesses have additional attributes, however, and I need to model this such that I can add roles that each have potentially different attributes. Instinct tells me that I need to have a separate model to represent each role's attributes, i.e. BusinessRoleProfile, ConsumerRoleProfile, etc and then maybe mixin a module programmatically that adds the appropriate has_one reference(s) to the profile model(s).
Is this the best way to handle the separate attributes? If so, can someone guide me through how to dynamically include those mixins based on what role the user has?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:
做了更多研究,这可能对您有帮助。 https://github.com/thefrontiergroup/scoped_attr_accessible
看起来你可以做类似的事情:
旧答案
看起来就像它可能会出现在 CanCan 2.0 中一样。
https://github.com/ryanb/cancan/issues/326
EDIT:
Did some more research, this may help you. https://github.com/thefrontiergroup/scoped_attr_accessible
Looks like you can do things like:
OLD ANSWER
Looks like it may be featured in CanCan 2.0.
https://github.com/ryanb/cancan/issues/326