10

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)
Lenin Raj Rajasekaran
  • 21,611
  • 14
  • 96
  • 136
lando2319
  • 1,327
  • 1
  • 13
  • 24
  • 1
    possible duplicate of [Rails 3 where condition using NOT NULL](http://stackoverflow.com/questions/4252349/rails-3-where-condition-using-not-null) – xlembouras Mar 08 '14 at 09:19

2 Answers2

14

Rails 4:

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

Rails 3 - Rails where condition using NOT NULL

Community
  • 1
  • 1
Lenin Raj Rajasekaran
  • 21,611
  • 14
  • 96
  • 136
8
Model.where(user_id: 14).where("foobar IS NOT NULL")

Model.where("user_id = ? AND foobar IS NOT ?", 14, nil)
Sampat Badhe
  • 8,042
  • 6
  • 31
  • 48