How to count all children records for a has_many association?
<p>Having the following models</p>

<pre><code>class Release
  has_many :sprints
end

class Sprint
  has_many :tasks
  belongs_to :release
end

class Task
  belongs_to :sprint
end
</code></pre>

<p>How can I count all the <code>tasks</code> for an specific <code>release</code>?</p>

<p>I tried this but it performs two queries and doesn't seem very efficient:</p>

<pre><code>def total_task_count
  release = Release.find(1)
  Task.where(sprint_id: release.sprints.pluck(:id)).count
end
</code></pre>

<p>What's a better way of counting all children associations in a child association?</p>

<p>Also, If I want to do this in a list view:</p>

<pre><code>@releases.each do |release|
  &lt;%= release.total_tasks_count %&gt;
end
</code></pre>

<p>How can I perform a single query instead of one per release?</p>