Shp*_*ord 43 sql postgresql scope ruby-on-rails
我有以下范围:
scope :comments, :conditions => ['text_value IS NOT NULL']
Run Code Online (Sandbox Code Playgroud)
但我也希望条件说"OR text_value IS not EMPTY"(或者那种效果).
我不想选择任何text_value
空/空的行.
Pat*_*ity 57
在Rails 4中你可以做到
where.not(text_value: '')
Run Code Online (Sandbox Code Playgroud)
Jor*_*ing 46
正如Erwin所指出的,text_value <> ''
在这种情况下,一个简单的比较就可以了.
scope :comments, where("text_value <> ''")
Run Code Online (Sandbox Code Playgroud)
(导轨3喜欢此查询语法scope
-as以及find
,all
等等-而不是一个选项散列例如:conditions => ...
,后者是在滑轨3.1弃用).
在Rails 4中,第二个参数应该是lambda:
scope :comments, ->{ where("text_value <> ''") }
Run Code Online (Sandbox Code Playgroud)
小智 30
铁轨4
scope :comments, -> { where.not(:text_value => nil) }
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
32988 次 |
最近记录: |