Rails 4:在用户模型中为没有角色的用户创建一个范围
Rails 4: create a scope in User model for Users with no role
我正在使用 Rolify gem,我需要获取所有在我的应用中没有角色的用户。如何在用户模型中创建一个范围来获取所有没有角色的用户?
User.with_no_role
你可以试试这个:
User.with_role(nil)
在 User
模型中,您可以定义范围:
class User < ActiveRecord::Base
scope :with_no_role, -> { where(role: nil) }
end
[编辑] 当你使用 Rolify 时,你需要做这样的事情:
User.where.not(id: User.with_role(:admin).pluck(users: :id))
见here
我正在使用 Rolify gem,我需要获取所有在我的应用中没有角色的用户。如何在用户模型中创建一个范围来获取所有没有角色的用户?
User.with_no_role
你可以试试这个:
User.with_role(nil)
在 User
模型中,您可以定义范围:
class User < ActiveRecord::Base
scope :with_no_role, -> { where(role: nil) }
end
[编辑] 当你使用 Rolify 时,你需要做这样的事情:
User.where.not(id: User.with_role(:admin).pluck(users: :id))
见here