一千萬個為什麽

搜索

Rails - using:include根據子項的屬性查找對象


我有一個句子和修正模型

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 句子 WHERE( correctionsentence_id IS NULL)

也許它的語法或者只是整體方法。有人可以幫忙嗎?

最佳答案

你可以用這個:

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

轉載註明原文: Rails - using:include根據子項的屬性查找對象

猜你喜歡