加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails设计 – 管理员角色,模型与属性

发布时间:2020-12-16 22:57:39 所属栏目:百科 来源:网络整理
导读:我知道如何创建管理员角色/用户: https://github.com/plataformatec/devise/wiki/How-To:-Add-an-Admin-role 我想知道的是,在决定它们时,两个选项是否有任何优点或缺点.任何人都可以提供任何有关此的见解吗? 解决方法 让我稍稍混淆水.我更喜欢通过Role表和
我知道如何创建管理员角色/用户: https://github.com/plataformatec/devise/wiki/How-To:-Add-an-Admin-role

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

解决方法

让我稍稍混淆水.我更喜欢通过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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读