是否可以在 Rails3 中反转命名范围? [英] Is it possible to invert a named scope in Rails3?

查看:37
本文介绍了是否可以在 Rails3 中反转命名范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Rails3 模型中,我有这两个命名范围:

In my Rails3 model I have these two named scopes:

scope :within_limit,     where("wait_days_preliminary <= ? ", ::WAIT_TIME_LIMIT.to_i )
scope :above_limit,      where("wait_days_preliminary > ? ",  ::WAIT_TIME_LIMIT.to_i )

基于它们的相似性,我很自然地通过反转第一个来定义第二个.

Based on their similarity, it would be natural for me to define the second by inverting the first.

我如何在 Rails 中做到这一点?

How can i do that in Rails?

推荐答案

Arel 有一个您可以使用的 not 方法:

Arel has a not method you could use:

condition = arel_table[:wait_days_preliminary].lteq(::WAIT_TIME_LIMIT.to_i)
scope :within_limit, where(condition)     # => "wait_days_preliminary <= x"
scope :above_limit,  where(condition.not) # => "NOT(wait_days_preliminary <= x)"

这篇关于是否可以在 Rails3 中反转命名范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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