【问题标题】:Get all admins CanCanCan获取所有管理员 CanCanCan
【发布时间】:2018-01-30 00:30:51
【问题描述】:

如何按角色确定用户的位置? 我想将所有具有以下角色的用户::admin 带到 users 表中。

用户的字段由 CanCanCan gem 生成,这是我的 user.rb

# == Schema Information
#
# Table name: users
#
...
#  roles_mask             :integer
...

class User < ApplicationRecord
  has_many :glucose_levels
  has_many :foods
  attr_accessor :current_password
  # has_secure_password

  ROLES = %i[patient, doctor]

  def roles=(roles)
    roles = [*roles].map { |r| r.to_sym }
    self.roles_mask = (roles & ROLES).map { |r| 2**ROLES.index(r) }.inject(0, :+)
  end

  def roles
    ROLES.reject do |r|
      ((roles_mask.to_i || 0) & 2**ROLES.index(r)).zero?
    end
  end

  def has_role?(role)
    roles.include?(role)
  end
  ...
end

【问题讨论】:

    标签: ruby-on-rails cancancan


    【解决方案1】:

    它与cancancan无关,您可以只做一个User.where(role: 'admin'),假设role是用户表中的字段名称。

    【讨论】:

    • 对不起,我已经添加了用户模型
    • @MatiasCarpintini。你能用你的用户表的模式更新问题吗
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多