简体   繁体   English

获取所有管理员CanCanCan

[英]Get all admins CanCanCan

How can I do a where of the users by role? 如何按角色对用户进行定位? I want to bring all the users with the role: :admin in the users table. 我想将所有具有角色的用户:admin加入用户表。

the fields for the users are generated by the CanCanCan gem, this is my user.rb 用户的字段由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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM