📎 Webclip
Getting to Know Pluck and Select
The post explains that select returns ActiveRecord model objects, while pluck returns plain values and skips the overhead of building models. It argues that this difference matters for performance even when query time looks similar.
Reading notes#
selectwas the standard way to query a single field beforepluckexisted.User.select(:id).to_areturns an array ofUsermodels with onlyidloaded.pluckwas introduced in Rails 3.2 to skip ActiveRecord object construction.User.pluck(:id)returns an array of integers instead of model instances.- The database query time can be similar, but object creation makes
selectslower. - A benchmark over 10,000 users showed
selectwas 91% slower thanpluck. pluckwas created to remove the overhead seen in patterns likeUser.select(:id).map(&:id).- As of Rails 4.0,
pluckcan take multiple columns. selectaccepts multiple columns as separate arguments or as an array, butpluckdoes not accept an array.selectis useful when the returned records need model methods.- The post summarizes the distinction as
pluckfor model values andselectfor model objects.
