Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using NOT operator with Squeel?

How do I write a SQL query using Squeel?

    SELECT * FROM documents where (NOT document.deleted OR document.deleted IS NULL)

I tried:

    Document.where { (-deleted) | (deleted == nil) }

but get this error:

    NoMethodError: undefined method `|' for deleted.-@:Squeel::Nodes::KeyPath
like image 501
mma Avatar asked Jun 29 '26 18:06

mma


1 Answers

It's very important to notice that ActiveRecord stores boolean values as string flags in the database. Therefore, in the case you mentioned, document.deleted equals 'f' which is a truthy value. That's why you can't simply use the NOT operator.

Perhaps what you're looking for is:

Document.where { deleted.eq(false) | deleted.eq(nil) }

Cheers

like image 77
Andre Bernardes Avatar answered Jul 01 '26 06:07

Andre Bernardes