选择蒙古语中不为空或为空的位置


80

我修改了模型,使其包含新的字段,例如...

field :url, :type => String

我使用activeadmin,因此在创建新条目时@model.url为空,而在更改架构之前创建的条目中为nil。如何选择两者?我努力了:

# Returns nils and strings
Model.where(:url.ne => "").count 

# Returns strings and ""
Model.where(:url.ne => nil).count

# Returns strings, nils and ""
Model.where(:url.ne => ["", nil]).count 

或者,如果有针对这种情况的最佳实践,请告诉我。

Answers:


84

尝试

Model.where(:url.nin => ["", nil]).count

即使在 url = nil


我同意,这是真正的答案,应该是该问题的公认答案。
奎因

如果您只想检查字段不为零的条目:Model.where(:field.ne=>nil)
Ekkstein '16


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.