Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveRecord IS NOT NULL sql finder

The following finder prints out IS NULL.

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

But I couldn't find how to print out IS NOT NULL one?

like image 242
Autodidact Avatar asked Aug 06 '11 12:08

Autodidact


3 Answers

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

This should do your job

like image 66
Wizz Avatar answered Nov 13 '22 09:11

Wizz


The simplest, idiomatic ActiveRecord solution:

User.where.not(id: nil)
like image 44
l3x Avatar answered Nov 13 '22 07:11

l3x


User.where.not(id: nil) will work for rails 4.

User.where("id IS NOT NULL") will work for older versions.

like image 5
Michael Sommer Avatar answered Nov 13 '22 09:11

Michael Sommer