Last Updated: February 25, 2016
·
952
· 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][pluck_update]. It wasn't until roughly seven months ago that [documentation was changed][pluckdocupdated] 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][]

[pluckupdate]: https://github.com/rails/rails/commit/6aae17e85613fe8c2816ba278f9348f168692479#activerecord/lib/activerecord/relation/calculations.rb
[pluckdocupdated]:https://github.com/rails/rails/commit/daab9bba887ec0682620087c39a0b4c097fc6efc#activerecord/lib/active_record/relation/calculations.rb
[Original Article]: http://blog.hashrocket.com/posts/rails-quick-tips-pluck-multiple-columns