I have the following scope:
scope :comments, :conditions => ['text_value IS NOT NULL']
But I also want the conditions to say "OR text_value IS NOT EMPTY" (or something to that effect).
I don't want to select any rows where text_value
is empty/blank.
As Erwin points out, a simple
text_value <> ''
comparison will work in this case.(Rails 3 prefers this query syntax for
scope
—as well asfind
,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:
In Rails 4 you can do
rails 4
Use
text_value <> ''
to efficiently cover both cases.Will only be
TRUE
for atext_value
that is neitherNULL
norempty
.Personally I am doing like this:
1) Add to initializers
2) Add to your model
Good luck!