在mongoid中选择不为null或空的位置(Select where not null or empty in mongoid)
我修改了一个模型,它包括一个新的字段,如...
field :url, :type => String
我使用了activeadmin,所以当我创建一个新的条目@model.url是空的,并且在更改模式之前创建的条目是零。 如何选择两者? 我努力了:
# 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或者,如果有这种情况的最佳做法,请让我知道。
I modified a model so it includes a new field, such as...
field :url, :type => String
I use activeadmin, so when I create a new entry @model.url is empty, and in entries created before changing the schema it's nil. How do I select both? I have tried:
# 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]).countOr, if there's a best practice for this kind of scenario please let me know.
最满意答案
尝试
Model.where(:url.nin => ["", nil]).count即使url = nil
Try
Model.where(:url.nin => ["", nil]).countIt works even when url = nil
更多推荐
发布评论