Rails - using:include根据子项的属性查找对象

时间:2010-04-19 01:42:55

标签: ruby-on-rails activerecord

我有一个句子和修正模型

class Sentence < ActiveRecord::Base
    has_one :correction

class Correction < ActiveRecord::Base
   belongs_to :sentence

我正在尝试查找所有没有更正的句子。为此,我只是寻找不存在的校正,即其id = nil。但它失败了,我无法弄清楚为什么

 Sentence.find :all, :include => :correction, :conditions => {:correction => {:id => nil}}

from (irb):4>> Sentence.find :all, :include => :correction, :conditions => {:correction => {:id => nil}}

ActiveRecord :: StatementInvalid:Mysql :: Error:'where子句'中的未知列'correction.sentence_id':SELECT * FROM sentences WHERE(correctionsentence_id IS NULL)

也许是语法或者只是整体方法。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:3)

您可以使用:

Sentence.all(:include => :correction, 
  :conditions => "corrections.sentence_id IS NULL")