2010-04-19 43 views
2

我有一個句子,修正模型導軌 - 使用:包括基於孩子的屬性

class Sentence < ActiveRecord::Base 
    has_one :correction 

class Correction < ActiveRecord::Base 
    belongs_to :sentence 

我試圖找到其中沒有校正所有句子找對象。要做到這一點,我只是尋找不存在的更正,即其ID =零。但它失敗,我想不通爲什麼

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

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

的ActiveRecord :: StatementInvalid的:mysql ::錯誤:未知列在 'where子句' correction.sentence_id「:SELECT * FROM sentences WHERE(correctionsentence_id IS NULL)

也許它的語法或可能只是整體的方法。誰能幫忙?

回答

3

您可以使用此:

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

問題即時得到這個錯誤 Sentence.all(:包括=>:校正, >:條件=> 「corrections.sentence_id IS NULL」) ActiveRecord :: StatementInvalid:Mysql :: Error:'where clause'中的未知列'correction.sentence_id':SELECT * FROM'句子WHERE('correction'.'sentence_id'爲NULL) – robodisco 2010-04-19 04:19:44

+0

看起來你指的是'更正'而不是'conditons'字符串中的'corrections'。 – 2010-04-19 04:50:17

+0

我知道了,謝謝你的幫助!救星。 – robodisco 2010-04-19 05:33:21