title: Counter Cache for a column with conditions?
body: <p>I am new to the concept of counter caching and with some astronomical load times on one of my app's main pages, I believe I need to get going on it.</p>
  <p>Most of the counter caches I need to implement have certain (simple) conditions attached. For example, here is a common query:</p>
  <pre><code>@projects = employee.projects.where(&quot;complete = ?&quot;, true).count
  </code></pre>
  <p>I am stumbling into the <code>N+1</code> query problem with the above when I display a form that lists the project counts for every employee the company has.</p>
  <h3>Approach</h3>
  <p>I don't really know what I'm doing so please correct me!</p>
  <pre><code># new migration
  add_column :employees, :projects_count, :integer, :default =&gt; 0, :null =&gt; false
  
  # employee.rb
  has_many :projects
  
  # project.rb
  belongs_to :employee, :counter_cache =&gt; true
  </code></pre>
  <p>After migrating... is that all I need to do?</p>
  <p>How can I work in the conditions I mentioned so as to minimize load times?</p>