问题
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 I'm not formatting the deleted attribute properly, but I'm not sure how to fix it.
Any help would be greatly appreciated. Thanks in advance.
Error message:
ActiveRecord::StatementInvalid in MailboxController#index
SQLite3::SQLException: no such column: true: SELECT "message_copies".* FROM "message_copies" WHERE ("message_copies".folder_id = 1) AND (deleted != true)
Application Trace:
app/controllers/mailbox_controller.rb:14:in `show'
app/controllers/mailbox_controller.rb:5:in `index'
Mailbox_Controller.rb
1 class MailboxController < ApplicationController
2 def index
3 current_user = User.find(session[:user_id])
4 @folder = Folder.where("user_id = #{current_user.id}").first
5 show
6 render :action => "show"
7 end
8
9 def show
10 current_user = User.find(session[:user_id])
11 @folder = Folder.where("user_id = #{current_user.id}").first
12 @msgs = @folder.messages
13 @ms = @msgs.where("deleted != true")
14 @messages = @ms.all.paginate :per_page => 10,
15 :page => params[:page], :include => :message,
16 :order => "messages.created_at DESC"
17 end
18 end
回答1:
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.
回答2:
@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)
回答3:
try
@ms = @msgs.where(["deleted != ?",true])
来源:https://stackoverflow.com/questions/5768364/activerecordstatementinvalid-sqlite3sqlexception-no-such-column-true