Rails scope for IS NOT NULL and is not empty/blank?

前端 未结 6 700
没有蜡笔的小新
没有蜡笔的小新 2021-01-30 05:20

I have the following scope:

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

But I also want the conditions to say \"OR text_val

6条回答
  •  我在风中等你
    2021-01-30 05:49

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

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

    (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.)

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

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

提交回复
热议问题