ActiveRecord::QueryMethods#select adds support for hash values in Rails 7.1


ActiveRecord provides an easy way to select specific fields from the table using the ActiveRecord::QueryMethods#select method.

Recently ActiveRecord::QueryMethods#select added support for hash values.

Let’s see some examples with the Product and Variant models

class Product < ApplicationRecord
  # id, name, description

  has_many :variants
end

class Variant < ApplicationRecord
  # id, price, quantity, product_id

  belongs_to :product
end

Before

Before 7.1, we were able to select specific fields with the following syntax:

Product
  .joins(:variants)
  .select("products.id, products.name, variants.price")

# OR

Product
  .joins(:variants)
  .select(:id, :name, "variants.price")

The above results in:

SELECT products.id, products.name, variants.price
FROM "products"
INNER JOIN "variants" ON "variants"."product_id" = "products"."id"
LIMIT $1  [["LIMIT", 11]]

/* OR */

SELECT "products"."id", "products"."name", "variants"."price"
FROM "products"
INNER JOIN "variants" ON "variants"."product_id" = "products"."id"
LIMIT $1  [["LIMIT", 11]]

After

After ActiveRecord::QueryMethods#select added support for hash 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.

Product
  .joins(:variants)
  .select(:id, :name, variants: [:price])

The above will result in:

SELECT "products"."id", "products"."name", "variants"."price"
FROM "products"
INNER JOIN "variants" ON "variants"."product_id" = "products"."id"
LIMIT $1  [["LIMIT", 11]]

We can also use the following syntax that supports alias:

Product
  .joins(:variants)
  .select(
    products: { id: :product_id, name: :product_name },
    variants: { price: :product_price }
  )

The above will result in:

SELECT "products"."id" AS product_id, "products"."name" AS product_name, "variants"."price" AS product_price
FROM "products"
INNER JOIN "variants" ON "variants"."product_id" = "products"."id"
LIMIT $1  [["LIMIT", 11]]

Join Our Newsletter