ruby-on-rails - ActiveRecord::StatementInvalid SQLite3::SQLException:没有这样的列:真:

标签 ruby-on-rails ruby-on-rails-3 sqlite sqlexception sqlite3-ruby

我想让@messages 返回@folder.messages,其中“deleted”列的值不等于true。我不确定为什么会一直抛出 SQLException。我想我没有正确格式化已删除的属性,但我不确定如何修复它。

如有任何帮助,我们将不胜感激。提前致谢。

错误信息:

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)  

应用程序跟踪:

app/controllers/mailbox_controller.rb:14:in `show'  
app/controllers/mailbox_controller.rb:5:in `index'  

邮箱 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  

最佳答案

SQLite 使用 C 风格 boolean values :

SQLite does not have a separate Boolean storage class. Instead, Boolean values are stored as integers 0 (false) and 1 (true).

所以,当你这样说时:

deleted != true

SQLite 不知道 true 是什么,所以它假定您正在尝试引用另一个列名。

处理此问题的正确方法是让 AR 将您的 Ruby bool 值转换为 SQLite bool 值(如 Tam 和 fl00r 的回答)。不过,我认为了解自己做错了什么很有用。

UPDATE:如果你想检查非真 deleted 并包含 NULL 那么你会想要这个:

@ms = @msgs.where("deleted != ? OR deleted IS NULL", true)

或者更好的是,根本不允许在 deleted 中使用 NULL。你不应该允许 NULL 是任何列,除非你绝对必须这样做(ActiveRecord 的默认为空性与它应该是的正好相反)。 SQL NULL 是一个奇怪的野兽,您总是必须特别对待它,最好不要允许它,除非您需要列的“不存在”或“未指定”值。

关于ruby-on-rails - ActiveRecord::StatementInvalid SQLite3::SQLException:没有这样的列:真:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5768364/

相关文章:

ruby-on-rails - Rails 3 中的嵌套表单更新将外键设置为 NULL

Android - 如何在 kotlin 的房间数据库中预填充数据(更新)?

ruby-on-rails-3 - 用于私有(private)存储库的 Travis CI

ruby-on-rails - Rails 3 数据建模帮助 - 具有许多、属于、嵌套属性

android - 记事本教程 : deleteDatabase() function

python - 具有可变数量占位符的安全 INSERT

ruby-on-rails - Rails FactoryGirl 错误 "undefined method ` 生成 LoremIpsum :Module (NoMethodError)"

ruby-on-rails - Rails 上周日相对于任何日期时间

mysql - 是否在 Controller Action 后使用after_filter,会缩短为用户生成最终 View 的时间?

ruby-on-rails - ArgumentError(缺少必需的 :bucket option):