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. It wasn't until roughly seven months ago that documentation was changed 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!

  • 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