Using the 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

      

+3


source to share


2 answers


It is very important to note that ActiveRecord stores boolean values ​​as string flags in the database. Therefore, in the mentioned case, document.deleted

equals 'f'

, which is the true value. Therefore, you cannot just use the operator NOT

.

Perhaps what you are looking for:



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

      

Greetings

+2


source


NOT

will change the returned set, so you have to define what you are comparing.

(-deleted) #there is nothing to reverse

      

Based on your comment I think you mean deleted not equal true

, therefore:



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

      

However, the opposite of true is false, right? So you can say:

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

      

+2


source







All Articles