Last Updated: February 25, 2016
·
927
· mattpolito

Rails Quick Tips - Pluck Multiple Columns

multi_pluck

Did you know that pluck was even 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.

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!

Original Article