Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails .where multiple values, one not nil

This Code pulls records where foobar is Nil

Model.where(user_id: 14, foobar: nil)

How can I use the same Syntax and get NOT nil?

Something like:

Model.where(user_id: 14, foobar: !nil)
like image 898
lando2319 Avatar asked Mar 08 '14 09:03

lando2319


2 Answers

Rails 4:

Model.where(user_id: 14).where().not(foobar: nil)

Rails 3 - Rails where condition using NOT NULL

like image 167
Lenin Raj Rajasekaran Avatar answered Oct 11 '22 23:10

Lenin Raj Rajasekaran


Model.where(user_id: 14).where("foobar IS NOT NULL")

Model.where("user_id = ? AND foobar IS NOT ?", 14, nil)
like image 8
Sampat Badhe Avatar answered Oct 11 '22 23:10

Sampat Badhe