title: Rails: How to get objects with at least one child?
body: <p>After googling, browsing SO and <a href="http://guides.rubyonrails.org/active_record_querying.html" rel="noreferrer">reading</a>, there doesn't seem to be a Rails-style way to efficiently get only those <strong><code>Parent</code> objects which have <em>at least one</em> <code>Child</code> object</strong> (through a <code>has_many :children</code> relation). In plain SQL:</p>
  
  <pre><code>SELECT *
    FROM parents
   WHERE EXISTS (
                 SELECT 1
                   FROM children
                  WHERE parent_id = parents.id)
  </code></pre>
  
  <p>The closest I've come is</p>
  
  <pre><code>Parent.all.reject { |parent| parent.children.empty? }
  </code></pre>
  
  <p>(based on <a href="https://stackoverflow.com/a/2737560/96588">another answer</a>), but it's really inefficient because it runs a separate query for each <code>Parent</code>.</p>