I want to have @messages return @folder.messages where the value of column \"deleted\" is NOT equal to true. I\'m not sure why this keeps throwing a SQLException. I guess
SQLite uses C-style boolean values:
SQLite does not have a separate Boolean storage class. Instead, Boolean values are stored as integers 0 (false) and 1 (true).
So, when you say this:
deleted != true
SQLite doesn't know what true
is so it assumes you're trying to reference another column name.
The proper way to deal with this is to let AR convert your Ruby boolean to an SQLite boolean (as in Tam's and fl00r's answers). I think it is useful to know what you're doing wrong though.
UPDATE: If you want to check for non-true deleted
and include NULL then you'll want this:
@ms = @msgs.where("deleted != ? OR deleted IS NULL", true)
Or better, don't allow NULLs in deleted
at all. You shouldn't allow NULL is any column unless you absolutely have to (ActiveRecord's default for nullability is exactly the opposite of what it should be). The SQL NULL is an odd beast and you always have to treat it specially, best not to allow it unless you need a "not there" or "unspecified" value for a column.
@ms = @msgs.where("deleted != ?", true)
# OR
@ms = @msgs.where(:deleted => false)
true
is different for different databases. In some it is t/f
value, and in some true/false
, so you should or place it in quotes and be sure if it is right for your particular database, or you should exclude it out of your sql so Rails will do the job for you.
UPD
If deleted
is NULL
. First. Set deleted field as a false by default. Second, how to find it with AR:
@ms = @msgs.where("deleted = ? OR deleted = ?", false, nil)
# wich won't work, Thanks to @mu is too short
@ms = @msgs.where("deleted = ? OR deleted IS NULL", false)
try
@ms = @msgs.where(["deleted != ?",true])