Active Record get parent records based on child data
<p>I have several related models: <code>Category</code> <code>has_many</code> <code>Question</code>. <code>Question</code> <code>has_many</code> <code>Answer</code>. I need:</p>

<ol>
<li>Get all Categories, where at least one <code>Question</code> has <code>Answer</code></li>
<li><p>Count all Questions, that has at least one <code>Answer</code>(how many Questions inside <code>Category</code> has Answers)</p></li>
<li><p>Get all Categories, where all Questions has 0 Answers</p></li>
<li>Count all Questions, that has not Answers ( How many Questions inside <code>Category</code>, that has not Answers).</li>
</ol>

<p>EDIT</p>

<p>If I want get list of categories with answers and list of categories without answers?</p>

<pre><code>cats = []
@categories_with_answers = Category.all.each do |cat|
  cats &lt;&lt; cat if cat.questions.joins(:answers).count &gt; 0
end
</code></pre>

<p>How I can write this snippet in more cleaner way?</p>