2011-08-06 12 views
13

El siguiente buscador imprime IS NULL.ActiveRecord IS NOT NULL buscador sql

User.where(:id =>nil) #=> [] 
User Load (0.5ms) SELECT `users`.* FROM `users` WHERE `users`.`id` IS NULL ORDER BY created_at DESC 

¿Pero no pude encontrar cómo imprimir IS NOT NULL one?

+1

Duplicate http://stackoverflow.com/questions/4252349/rail-3-where-condition-using-not-null – Vache

+0

Esta pregunta tiene su respuesta - http://stackoverflow.com/questions/4252349/rail-3- where-condition-using-not-null – ipr101

Respuesta

15

La solución ActiveRecord idiomática más simple:

User.where.not(id: nil) 
+1

Nota : Solo funciona con 'rails> = v4.0.2'. Ver: http://apidock.com/rails/ActiveRecord/QueryMethods/WhereChain/not – morgents

26

Trate User.where("id IS NOT NULL")

Esto debería hacer su trabajo

5

User.where.not(id: nil) funcionará para los carriles 4.

User.where("id IS NOT NULL") trabajará para las versiones anteriores.

+0

Esto es más o menos lo mismo que http://stackoverflow.com/a/27613775/38765 (aunque usted respondió primero), y tampoco funciona en Rails 3. –

0

Carriles 4

puede probar esta

User.where("id is not NULL") 

Además, si usted está buscando para excluir determinado Identificación e incluir valores nulos siguientes voluntad trabajo

User.where("id is NULL or id != 'excluded_id'")