Rails sqlite字符串相等不起作用

时间:2011-09-11 12:09:33

标签: 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: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\")" 

ree-1.8.7-2011.03:040&gt;

我正在使用rails 3.0.9和ree-1.8.7

这是我的模特

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')"

1 个答案:

答案 0 :(得分:0)

Caption.joins(:flags).where("captions.status != ?", "safe")

Caption.joins(:flags).where("captions.status != ? OR captions.status IS NULL", "safe")