ActiveRecord::Base#pluck adds support for hash values in Rails 7.2

In day-to-day life, we see many dashboards which consist of charts and reports. To build these charts or reports quickly, we required specific data from different database tables in faster queries.

The ActiveRecord::Base#pluck method is used to query single or multiple attributes from the database without loading the entire record. It returns the results as an array of attribute values.

User.pluck(:id, :name, :email)

The above code will result in the following query:

SELECT users.id, users.name, users.email FROM "users"

The above query returns the following output:

=>
  [
    [1, "David Heinemeier Hansson", "[email protected]"],
    [2, "Rafael França", "[email protected]"],
    [3, "Vipul Amler", "[email protected]"]
  ]

Before

We are not limited to querying fields from a single table, we can query multiple tables as well.

While we can use the column names as a symbol it used to only work while fetching columns from a single table, but when we wanted to join multiple tables we had to go back to the string format to mention the table names.

Before Rails 7.2, we were able to fetch specific fields with the following syntax:

Employee
  .joins(:companies)
  .pluck("employees.id, employees.name, companies.id, companies.name")

The above code will result in the following query:

SELECT employees.id, employees.name, companies.id, companies.name
FROM "employees"
INNER JOIN "companies"
ON companies.id = employees.company_id

The above query returns the following output:

=>
  [
    [1, "David Heinemeier Hansson", 1, "37signals"],
    [2, "Jason Fried", 1, "37signals"],
    [3, "Rafael França", 2, "Shopify"],
    [4, "Vipul Amler", 3, "Saeloun"]
  ]

After

From Rails 7.2, We can use ActiveRecord::Base#pluck to accept hash values. So, we don’t need to use the raw version of the query anymore.

Let’s take a look at the same code after this change.

Employee
  .joins(:companies)
  .pluck(employees: [:id, :name], companies: [:id, :name])

The above code will result in the following query:

SELECT employees.id, employees.name, companies.id, companies.name
FROM "employees"
INNER JOIN "companies"
ON companies.id = employees.company_id

The same applies to pick, which is implemented using pluck.

pick is short-hand for relation.limit(1).pluck(*column_names).first, and is primarily useful when we have a relation that’s already narrowed down to a single row.

Employee
  .joins(:companies)
  .where(id: 1)
  .pick(employees: [:id, :name], companies: [:id, :name])

The above code will result in the following query:

SELECT employees.id, employees.name, companies.id, companies.name
FROM "employees"
INNER JOIN "companies"
ON companies.id = employees.company_id
WHERE employees.id = 1
LIMIT 1

The above query returns the following output:

=> [1, "David Heinemeier Hansson", 1, "37signals"]

In the same way, Select and Reselect also accepts the hash values.

Need help on your Ruby on Rails or React project?

Join Our Newsletter