Ruby on Rails: pluck x select x map

Flavio Wuensche
5 min readJan 30, 2023

I’m writing this short article to clarify a few questions about Active Record vs Ruby methods to manipulate data coming from the database.

I'll start with some straightforward cases. Let's suppose you're looking for a list of emails from our user accounts. There are several ways you can achieve the same outcome. We'll start with the pluck method.

About :pluck, from Active Record

Account.all.pluck(:email)

# triggered query
SELECT "accounts"."email" FROM "accounts"

# returned values
=> ["admin_arthur@doctolib.com",
"ivan@doctolib.com",
"jessy@doctolib.com"]

Using pluck will retrieve the names from users and return them in an array of strings. No unused columns. No model initialization. For this use case, pluck is hands down the best choice, but let's deep dive into select too.

About :select, from Active Record

Here's where the confusion often starts. There's also a select method in Ruby, which can be used to filter items of a collection based on a certain condition. This is not the same as the select method from ActiveRecord which specifies the columns of a table to be selected.

Account.all.select(:email)

# triggered query
SELECT "accounts"."email" FROM "accounts"

# apparent returned…

--

--

Flavio Wuensche

Building an open-source tool to keep track of technical debt on large codebases 🍒 cherrypush.com