Seleccione donde no es nulo o vacío en mongoid


He modificado un modelo para que incluya un nuevo campo, como...

field :url, :type => String

Uso activeadmin, por lo que cuando creo una nueva entrada @model.url está vacía, y en las entradas creadas antes de cambiar el esquema es nil. ¿Cómo selecciono ambos? He intentado:

# 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 

O, si hay una mejor práctica para este tipo de escenario por favor hágamelo saber.

Author: Duopixel, 2012-04-26

3 answers

Intenta

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

Funciona incluso cuando url = nil

 73
Author: a14m,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-24 07:54:40

Intenta

Model.where(:url.ne => "", :url.exists => true).count

Ver Operadores de Símbolos Mongoides

 89
Author: Kyle,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-07-02 16:48:17

Intenta:

Model.nin(url: ['', nil])
 1
Author: p.matsinopoulos,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-04-13 14:28:41