Rails - using: include to find objects based on their child attributes

I have a suggestion and fix model

class Sentence < ActiveRecord::Base
    has_one :correction

class Correction < ActiveRecord::Base
   belongs_to :sentence

      

and I am trying to find all the suggestions that have no correction. For this I am just looking for fixes that do not exist i.e. Id = nil. But it fails and I can't figure out why

 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: Unknown column "correct.sentence_id" in clause "where": SELECT * FROM sentences

WHERE ( correction

. sentence_id

IS NULL)

Perhaps this is syntax or maybe a general approach. Can anyone please help?

+2


a source to share


1 answer


You can use this:



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

      

+3


a source







All Articles