title: How do I get Rails to eager load counts?
body: <p>This is related to a question <a href="https://stackoverflow.com/q/1180466/203137">a year and change ago</a>.</p>
  
  <p>I put up an example of the question that should work out of the box, provided you have sqlite3 available: <a href="https://github.com/cairo140/rails-eager-loading-counts-demo" rel="nofollow noreferrer">https://github.com/cairo140/rails-eager-loading-counts-demo</a></p>
  
  <h2>Installation instructions (for the main branch)</h2>
  
  <pre><code>git clone git://github.com/cairo140/rails-eager-loading-counts-demo.git
  cd rails-eager-loading-counts-demo
  rails s
  </code></pre>
  
  <p>I have a fuller write-up in the repository, but my general question is this.</p>
  
  <p><strong>How can I make Rails eager load counts in a way that minimizes db queries across the board?</strong></p>
  
  <p>The <code>n+1</code> problem emerges whenever you use <code>#count</code> on an association, despite having included that association via <code>#includes(:associated)</code> in the ActiveRelation. A workaround is to use <code>#length</code>, but this works well only when the object it's being called on has already been loaded up, not to mention that I suspect it duplicates something that the Rails internals have done already. Also, an issue with using <code>#length</code> is that it results in an unfortunate over-loading when the association was not loaded to begin with and the count is all you need.</p>
  
  <p>From the readme:</p>
  
  <blockquote>
    <p>We can dodge this issue by running #length on the posts array (see appendix), which is already loaded, but it would be nice to have count readily available as well. Not only is it more consistent; it provides a path of access that doesn't necessarily require posts to be loaded. For instance, if you have a partial that displays the count no matter what, but half the time, the partial is called with posts loaded and half the time without, you are faced with the following scenario:</p>
    
    <ul>
    <li>Using <code>#count</code>
    
    <ul>
    <li>n <code>COUNT</code> style queries when posts are already loaded</li>
    <li>n <code>COUNT</code> style queries when posts are not already loaded</li>
    </ul></li>
    <li>Using <code>#length</code>
    
    <ul>
    <li>Zero additional queries when posts are already loaded</li>
    <li>n <code>*</code> style queries when posts are not already loaded</li>
    </ul></li>
    </ul>
    
    <p>Between these two choices, there is no dominant option. But it would be nice to revise #count to defer to #length or access the length that is some other way stored behind the scenes so that we can have the following scenario:</p>
    
    <ul>
    <li>Using revised <code>#count</code>
    
    <ul>
    <li>Zero additional queries when posts are already loaded</li>
    <li>n <code>COUNT</code> style queries when posts are not already loaded</li>
    </ul></li>
    </ul>
  </blockquote>
  
  <p>So what's the correct approach here? Is there something I've overlooked (very, very likely)?</p>