Column alias for ActiveRecord select in Rails 7.1
Previously, when writing an ActiveRecord select and having the need to add a column alias, you'd have to provide a SQL fragment string like so:
Customer.select("customers.name AS customer_name")
=> SELECT customers.name AS customer_name FROM "customers"
Rails 7.1 added a more robust hash syntax for selects that also allows for column aliasing.
Customer.joins(:orders).select(name: :customer_name)
=> SELECT "customers"."name" AS "customer_name", FROM "customers"
Tweet