title: (Rails) When to use ActiveRecord's .where and .select
body: <p>I have a Model in my Rails app named <code>User</code>, which has an attribute <code>name</code></p>
  
  <p>Let's say I want to find all <code>Users</code> whose <code>name</code> matches a specific string.</p>
  
  <p>In a Rails Console, I could do:</p>
  
  <p>OPTION 1</p>
  
  <p><code>User.where("name LIKE 'string'")</code></p>
  
  <p>This is very fast.</p>
  
  <p>OPTION 2</p>
  
  <p><code>User.select{|u| u.name == "string"}</code></p>
  
  <p>This is extremely slow. It works for a small database, but if you have hundreds of thousands of users, this appears to try to load all <code>Users</code> into memory first, and then iterate them in the block.</p>
  
  <p>Does this mean that option 2 is always wrong ? What's the correct use case for <code>.select</code> then, when is it preferable over <code>.where</code> ? </p>
  
  <p>I wrote a bunch of code in my app that is working fine using <code>.select</code>, but now that I tried using over a very large table I saw that I might be doing something wrong. </p>