title: ActiveRecord: How to find parents whose ALL children match a condition?
body: <p>Suppose I have a <code>Parent</code> model that has many <code>Child</code>, and that <code>Child</code> also belongs to <code>OtherParent</code>.</p>
  
  <p>How can i find all <code>Parent</code> where ALL of its <code>Child</code> belongs to any <code>OtherParent</code>?</p>
  
  <p>In pure SQL I could do </p>
  
  <pre><code>Parent.find_by_sql(&lt;&lt;SQL)
    SELECT *
    FROM parents p
    WHERE NOT EXISTS (
      SELECT *
      FROM children
      WHERE parent_id = p.id
        AND other_parent_id IS NULL
    )
  SQL
  </code></pre>
  
  <p>(from <a href="https://stackoverflow.com/questions/6430761/rails-how-to-query-for-all-the-objects-whose-every-association-have-an-attribut">here</a>), but I'd prefer to do it by taking advantage of ActiveRecord if possible.</p>
  
  <p>Thanks!</p>
  
  <hr>
  
  <p>I'm using Rails 4.2.1 and PostgreSQL 9.3</p>