如果预定义方法等于true,我正试图从users表中获取实体。
我的用户模型中有这个方法
def self.expert?
return self.has_role? :domain_expert
end
我正在尝试进行此查询
users = User.where(:expert? => true)
错误
no such column: users.expert?
如何进行此查询?
编辑:
我正在使用rolify gem
在角色模型中
has_and_belongs_to_many :users, :join_table => :users_roles
答案 0 :(得分:2)
如果您使用高于3.2的Rolify宝石版本,那么它只是
User.with_role(:domain_expert)
如果您按照此处所述设置了cancan角色:Role Based Authorization
然后你应该像
一样查询User.where(:role => :domain_expert)
您还可以为此定义范围,并将其命名为User.domain_experts
如果您在3.2版之前使用Rolify gem,或者像此处所述制作单独的模型:Separate Role Model
然后你应该像
一样查询Role.where(:name => :domain_expert).users
答案 1 :(得分:0)
查看rolify gem自述文件,有一个with_role
范围
User.with_role(:domain_expert)
答案 2 :(得分:0)
最后我明白了:)。
我必须加入两个表(用户和角色)
User.joins(:roles).where("name = ?", "domain_expert")
这解决了我的问题:)。