如何在命名范围内有多个条件? [英] How to have multiple conditions in a named scope?

查看:15
本文介绍了如何在命名范围内有多个条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户模型.我可以通过执行 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 分钟内更新,但我如何合并管理员检查?我不知道如何在范围内的用户实例上调用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆