Heading image for post: Rails Quick Tips - Pluck Multiple Columns

Ruby

Rails Quick Tips - Pluck Multiple Columns

Profile picture of Matt Polito

Did you know that pluck was more powerful than previously thought? Who woulda thunk it!? While going through the source I realized that you can actually request multiple columns to be plucked instead of just one.

Read my previous post on ActiveRecord::Calculations.pluck

User.pluck(:email, :active)
=> [['dude@example.com', true], ['sweet@example.com', false]]

Whoo, this is pretty cool. Must be new. Well turns out, it isn't. Looks like it was [added about a year ago][pluck_update]. It wasn't until roughly seven months ago that [documentation was changed][pluck_doc_updated] to reflect the added functionality. It is available in Rails 4, but can be added to Rails 3.2.x with an ActiveRecord extension.

Moral of the story here is go spelunking through code. Not only will it provide you with extra understanding of the software around you but you may also find untold riches!

Thank you to all those who provided this functionality!

[pluck_update]: https://github.com/rails/rails/commit/6aae17e85613fe8c2816ba278f9348f168692479#activerecord/lib/active_record/relation/calculations.rb [pluck_doc_updated]:https://github.com/rails/rails/commit/daab9bba887ec0682620087c39a0b4c097fc6efc#activerecord/lib/active_record/relation/calculations.rb

  • Adobe logo
  • Barnes and noble logo
  • Aetna logo
  • Vanderbilt university logo
  • Ericsson logo

We're proud to have launched hundreds of products for clients such as LensRentals.com, Engine Yard, Verisign, ParkWhiz, and Regions Bank, to name a few.

Let's talk about your project