如何在命名范围内具有多个条件? [英] How to have multiple conditions in a named scope?
本文介绍了如何在命名范围内具有多个条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个用户模型.我可以通过执行a_user.try(:admin?)
来检查用户是否是管理员.
I have a User model. I can check whether a User is an admin by doing a_user.try(:admin?)
.
我想定义一个命名作用域,该作用域将在最近X分钟内更新所有不是 管理员的用户.到目前为止,我有:
I'd like to define a named scope that gets all Users updated within the last X minutes who are not admins. So far I have:
scope :recent, lambda { { :conditions => ['updated_at > ?', 5.minutes.ago] } }
此操作成功在最近5分钟内更新了所有用户,但是如何合并管理员检查?我不知道如何在范围内的User实例上调用try()
...
This successfully gets all Users updated within the last 5 minutes, but how do I incorporate the admin check? I don't know how to call try()
on an instance of a User inside the scope...
推荐答案
如果users表中的admin列是布尔值,
if admin column in users table is a boolean,
scope :recent, lambda { :conditions => ['updated_at > ? AND admin != ?', 5.minutes.ago, true] }
这篇关于如何在命名范围内具有多个条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文