title: Rails Activerecord: Map vs pluck vs as_json
body: <p>My question is purely based on the performance impact of the 3 approaches. I need to know which is the best method to use for the task at hand.</p>
  
  <p>I need to render json for a specific set of records.</p>
  
  <p>The current approach I use is</p>
  
  <pre><code>result = User.all.map{ |u| { name: u.name, id: u.id } }
  </code></pre>
  
  <p>As you may know, this will loop through Activerecord objects and this will have very low performance for a large set of records.</p>
  
  <p>Alternative approach is to use pluck.</p>
  
  <pre><code>result = User.all.pluck(:name,:id).map{ |u| { name: u[0], id: u[1] } }
  </code></pre>
  
  <p>Probably much better in performance, but code looks smelly.</p>
  
  <p>The same can be achieved with as_json if we override as_json as include only the necessary parameters but I need to know if this will also iterate through activerecord objects.</p>
  
  <p>Feel free to say if there is any other approach which will give better performance.</p>