在mongoid中选择不为null或空的位置(Select where not null or empty in mongoid)

系统教程 行业动态 更新时间:2024-06-14 16:57:39
在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]).count

Or, 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]).count

It works even when url = nil

更多推荐

本文发布于:2023-04-13 11:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/3fd7ca096cb18fb94adaa507f2a12233.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不为   位置   mongoid   null   empty

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!