Hashrocket.com / blog

Large rails quick tip

Rails Quick Tips - Pluck Multiple Columns

posted on and written by in

Image 100x100 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!

Posted in Rails Quick Tips and tagged with Ruby, Rails Quick Tips