为Rails中具有不同属性的角色的用户建模的最佳方式



我是Rails的新手,希望有人能帮我解决这个问题。我有一个应用程序,它有一个单一的用户模型,使用Authlogic进行身份验证,使用CanCan进行授权。有三个角色:消费者、业务和管理员。用户可以拥有任意数量的这些角色。

然而,企业有额外的属性,我需要对此进行建模,以便添加每个角色都具有潜在的不同属性。Instinct告诉我,我需要一个单独的模型来表示每个角色的属性,即BusinessRoleProfile、ConsumerRoleProfile等,然后可以通过编程方式混合一个模块,将适当的has_one引用添加到配置文件模型中。

这是处理单独属性的最佳方式吗?如果是这样的话,有人能指导我如何根据用户的角色动态地包括这些mixin吗?

编辑:

多做一些研究,这可能会对你有所帮助。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

最新更新