在 Rails 中对具有不同属性的角色进行建模的最佳方法是什么?

发布于 2024-11-10 03:46:19 字数 351 浏览 4 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

八巷 2024-11-17 03:46:19

编辑:

做了更多研究,这可能对您有帮助。 https://github.com/thefrontiergroup/scoped_attr_accessible

看起来你可以做类似的事情:

class User < ActiveRecord::Base

  # All attributes are accessible for the admin scope.
  attr_accessible :all, :scope => :admin

  # The default scope can only access a and b.
  attr_accessible :a, :b

  # Make both :c and :d accessible for owners and the default scope
  attr_accessible :c, :d, :scope => [:owner, :default]

  # Also, it works the same with attr_protected!
  attr_protected :n, :scope => :default

end

旧答案

看起来就像它可能会出现在 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:

class User < ActiveRecord::Base

  # All attributes are accessible for the admin scope.
  attr_accessible :all, :scope => :admin

  # The default scope can only access a and b.
  attr_accessible :a, :b

  # Make both :c and :d accessible for owners and the default scope
  attr_accessible :c, :d, :scope => [:owner, :default]

  # Also, it works the same with attr_protected!
  attr_protected :n, :scope => :default

end

OLD ANSWER

Looks like it may be featured in CanCan 2.0.

https://github.com/ryanb/cancan/issues/326

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文