title: Rails3: Find records that have a relationship but exclude some by a specific value
body: <p>Hi have two models (Rails3)</p>
  
  <p>Aunt
  Friends</p>
  
  <p>Where </p>
  
  <blockquote>
    <p>aunt :has_many => friends</p>
  </blockquote>
  
  <p>and </p>
  
  <blockquote>
    <p>friend :belongs_to => aunt</p>
  </blockquote>
  
  <p>Now, I want to get all aunts that are not related to friend X</p>
  
  <p>I tried something like:</p>
  
  <blockquote>
    <p>Aunt.includes(:friends).where('friends.id != ? ', X )</p>
  </blockquote>
  
  <p>But it doesn't work.</p>
  
  <p>PS: I also need to include the aunts that don't have any friends associated.</p>
  
  <p>This is the output of :</p>
  
  <pre><code>@results = Aunt.custom(true).select('aunts.id, aunts.goal').joins("LEFT OUTER JOIN friends r ON aunts.id = r.aunt_id").where('r.other_value != ? or r.id is NULL', 4259 ).ordered_by_status_creation.select('aunts.id, aunts.goal')
  </code></pre>
  
  <blockquote>
    <p>SELECT aunts.id, aunts.goal FROM <code>aunts</code> LEFT OUTER JOIN friends r ON
    aunts.id = r.aunt_id WHERE <code>aunts</code>.<code>deleted_at</code> IS NULL AND
    <code>leads</code>.<code>type</code> IN ('MyType') AND (is_custom = 1) AND (r.another_value
    != 4259 or r.id is NULL) ORDER BY status desc, aunts.id desc</p>
  </blockquote>
  
  <p>Any ideas?</p>