Rails范围是否为IS NOT NULL且不为空/空白? [英] Rails scope for IS NOT NULL and is not empty/blank?

查看:90
本文介绍了Rails范围是否为IS NOT NULL且不为空/空白?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下范围:

scope :comments, :conditions => ['text_value IS NOT NULL']

但我也希望条件说或text_value不为空空(或类似的意思)。

But I also want the conditions to say "OR text_value IS NOT EMPTY" (or something to that effect).

我不想选择 text_value 是的任何行

推荐答案

正如欧文(Erwin)所指出的,一个简单的 text_value< 比较在这种情况下将起作用。

As Erwin points out, a simple text_value <> '' comparison will work in this case.

scope :comments, where("text_value <> ''")

(Rails 3在 scope中更喜欢此查询语法以及 find all 等,而不是选项哈希,例如:conditions => ... 。后者已已弃用在Rails 3.1中。)

(Rails 3 prefers this query syntax for scope—as well as find, all, etc.—rather than an options hash e.g. :conditions => .... The latter is deprecated in Rails 3.1.)

在Rails 4中,第二个参数应该是lambda:

In Rails 4, the second argument should be a lambda instead:

scope :comments, ->{ where("text_value <> ''") }

这篇关于Rails范围是否为IS NOT NULL且不为空/空白?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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