Ruby on rails Rails sqlite字符串相等不起作用

Ruby on rails Rails sqlite字符串相等不起作用,ruby-on-rails,ruby-on-rails-3,sqlite,Ruby On Rails,Ruby On Rails 3,Sqlite,为什么这个查询不能创建正确的结果 ree-1.8.7-2011.03 :037 > Caption.joins(:flags) => [#<Caption id: 3, text: "another another caption", point: 1, up: 1, down: 0, submission_id: 1, user_id: 1, created_at: "2011-09-11 10:19:38", updated_at: "2011-09-11 12:04:0

为什么这个查询不能创建正确的结果

ree-1.8.7-2011.03 :037 > Caption.joins(:flags)
 => [#<Caption id: 3, text: "another another caption", point: 1, up: 1, down: 0, submission_id: 1, user_id: 1, created_at: "2011-09-11 10:19:38", updated_at: "2011-09-11 12:04:03", flags_count: 1, status: nil>] 

ree-1.8.7-2011.03 :035 > Caption.joins(:flags).where('status != "safe"')
 => [] 

ree-1.8.7-2011.03 :036 > Caption.joins(:flags).where('status != "safe"').to_sql
 => "SELECT \"captions\".* FROM \"captions\" INNER JOIN \"flags\" ON \"flags\".\"caption_id\" = \"captions\".\"id\" WHERE (status != \"safe\")" 

ree-1.8.7-2011.03 :038 > Caption.joins(:flags).where('status <> "safe"')
 => [] 

ree-1.8.7-2011.03 :039 > Caption.joins(:flags).where('status <> "safe"').to_sql
 => "SELECT \"captions\".* FROM \"captions\" INNER JOIN \"flags\" ON \"flags\".\"caption_id\" = \"captions\".\"id\" WHERE (status <> \"safe\")" 


状态
字段在哪里?在
标题
标志
表上?哦,我现在看到了,您的第二个查询完成了任务。我想是吧!=如果列为null,“safe”不返回true
class Caption < ActiveRecord::Base
  belongs_to :submission
  belongs_to :user
  has_many :votes
  has_many :flags

  validates_presence_of :submission_id
  validates_presence_of :user_id
  validates_inclusion_of :status, :in => ['safe', nil]
end


class Flag < ActiveRecord::Base
  belongs_to :caption, :counter_cache => true
  belongs_to :user
end
ree-1.8.7-2011.03 :002 > Caption.joins(:flags).where("captions.status != ?", "safe")
 => [] 

ree-1.8.7-2011.03 :003 > Caption.joins(:flags).where("captions.status != ?", "safe").to_sql
 => "SELECT \"captions\".* FROM \"captions\" INNER JOIN \"flags\" ON \"flags\".\"caption_id\" = \"captions\".\"id\" WHERE (captions.status != 'safe')"
Caption.joins(:flags).where("captions.status != ?", "safe")
Caption.joins(:flags).where("captions.status != ? OR captions.status IS NULL", "safe")