ActiveRecord query: where a field is not true

I want to find all records where a field is not true. The working AR syntax for this is:

Dog.where(:stray => [false, nil])

Is there a less verbose way of querying for 'not true'? It really sucks having to cater for this mysql nuance everywhere.

Answers


I think you can write Dog.where(Dog.arel_table[:stray].not_eq(true)).

As a side note, I'd recommend that you set a default value for the stray column in the database, or at least require a value for it. That way you wouldn't have to make this workaround.


How about a named scope?

scope :not_stray, where("stray IS NULL OR stray = false")

Then use:

Dog.not_stray


Need Your Help

KeychainWrapperItem stopped working in iOS 7.1

ios keychain keychainitemwrapper

I recently installed iOS 7.1 simulator and the new Xcode 5.1. My App worked in iOS 7 fine. I'm using the KeychainItemWrapper class from Apple.

read a JSON object in java

java json jsp

How can we read a JSON object in a JSP, Servlet or in any other Java program?