title: Rails: Get count of values from database
body: <p>I have a column in my database called "experience" the user can save values called 0-2 years, 2-5 years, 5-10 years, 10+ years.  My question is how do I get a count of those values?  I'm not looking for a sum total of all, I'm trying to get a count of how many "2-5 years" values exist in the column and present that to the user in the view.</p>
  
  <p>So in the view the user will see the values from the database tables column called experience and then how many jobs are posted and contain that value like this. </p>
  
  <p><strong>Experience:</strong></p>
  
  <p>0-2 years (3)</p>
  
  <p>2-5 years (15)</p>
  
  <p>Currently I am doing this in the view</p>
  
  <pre><code>&lt;%= link_to "0-2 years", filtered_jobs_path(experience: '0-2 years', num_days_past: params[:num_days_past], search: params[:search]) %&gt;
     (&lt;%= Job.where(experience: '0-2 years').size %&gt;)&lt;br /&gt;
  </code></pre>
  
  <p>I was reading it's better to use size than count because size won't make another call to the database if it's already been loaded.  I'm not sure if I'm doing this in the most efficient way though, possibly I should be using a counter cache column?</p>
  
  <p>Here are the calls that get made how I currently have it setup, seems like the database is constantly being queried even though I'm using .size</p>
  
  <pre><code>   (0.1ms)  SELECT COUNT(*) FROM "jobs" WHERE "jobs"."experience" = ?  [["experience", "0-2 years"]]
  
     (0.1ms)  SELECT COUNT(*) FROM "jobs" WHERE "jobs"."experience" = ?  [["experience", "2-5 years"]]
  
     (0.1ms)  SELECT COUNT(*) FROM "jobs" WHERE "jobs"."experience" = ?  [["experience", "5-10 years"]]
  
     (0.1ms)  SELECT COUNT(*) FROM "jobs" WHERE "jobs"."experience" = ?  [["experience", "10+ years"]]
  
    Job Load (0.2ms)  SELECT  "jobs".* FROM "jobs" LIMIT ? OFFSET ?  [["LIMIT", 5], ["OFFSET", 0]]
  
     (0.1ms)  SELECT COUNT(*) FROM "jobs"
  
    CACHE (0.0ms)  SELECT COUNT(*) FROM "jobs"
  </code></pre>