Question: ❓❓❓

Using the rails 3 style how would I write the opposite of:


I want to find where id is NOT null. I tried:


But that returns:

=> "SELECT     \"foos\".* FROM       \"foos\"  WHERE  (\"bars\".\"id\" = 1)"

That’s definitely not what I need, and almost seems like a bug in ARel.

Good Answer: 👌👌👌

The canonical way to do this with Rails 3:

Foo.includes(:bar).where(" IS NOT NULL")

ActiveRecord 4.0 and above adds where.not so you can do this:

Foo.includes(:bar).where.not('' => nil)
Foo.includes(:bar).where.not(bars: { id: nil })

I no longer recommend using Squeel for this as it doesn’t always have a full-time maintainer, and it depends on private APIs that cause frequent breaking changes.