Wybierz gdzie nie jest null lub empty w mongoid

Zmodyfikowałem model tak, aby zawierał nowe pole, takie jak...

field :url, :type => String

Używam activeadmin, więc kiedy tworzę nowy wpis {[2] } jest pusty, a we wpisach utworzonych przed zmianą schematu jest zero. Jak wybrać oba? Próbowałem:

# 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 

Lub, jeśli jest najlepsza praktyka dla tego rodzaju scenariusz proszę dać mi znać.

Author: Duopixel, 2012-04-26

3 answers

Try

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

Działa nawet wtedy, gdy 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/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-24 07:54:40

Try

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

Zobacz Operatory Symboli Mongoid

 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/doraprojects.net/template/agent.layouts/content.php on line 54
2014-07-02 16:48:17

Try:

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/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-13 14:28:41