Rails设计 - 管理员角色,模型与属性

Mr *_*phe 6 ruby ruby-on-rails devise

我知道如何创建管理员角色/用户:https://github.com/plataformatec/devise/wiki/How-To : -Add- an-Admin-role

我想知道的是,在决定它们时,两个选项是否有任何优点或缺点.任何人都可以提供任何有关此的见解吗?

tih*_*hom 6

让我稍稍混淆水.我更喜欢通过Role表和连接表UserRole.这样我就可以定义多个角色而无需向db添加另一个列/表.

class User
  has_many :user_roles
  has_many :roles, :through => :user_roles

  def role?(role)
    role_names.include? role
  end

  def role_names
    @role_names ||= self.roles.map(&:name)
  end

  def role=(role)
    self.roles << Role.find_or_create_by_name(role)
  end
end

class UserRole 
  # integer: user_id, integer: role_id
  belongs_to :user
  belongs_to :role
end

class Role
  # string: name
  has_many :user_roles
  has_many :users, :through => :user_roles
end
Run Code Online (Sandbox Code Playgroud)