ActiveRecord::QueryMethods#select adds support for hash values in Rails 7.1
source link: https://blog.saeloun.com/2022/09/07/activerecord-select-adds-support-for-hash-values-in-rails-7
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
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]]
Recommend
-
6
Rails adds #excluding method for an ActiveRecord::Relation to exclude specific record (or collection of records) from the resulting relation. We come ac...
-
7
Rails 6.1 adds ActiveRecord methods `#sole` and `#find_sole_by` Mar 16, 2021 , by Swaathi Kakarla 1 minute read Inspired by Django’s
-
5
Rails 7 adds invert_where method to ActiveRecord Apr 21, 2021 , by Mayank Khanna 2 minute read We often come across cases in...
-
9
Rails 7 adds encryption to ActiveRecord Jun 9, 2021 , by Mayank Khanna 1 minute read Before Rails 7, we used a lot of gems l...
-
5
Rails 7 adds previously_persisted? method to ActiveRecord Jul 6, 2021 , by Nishant Samel 1 minute read Rails has provided m...
-
5
Rails 7 adds ActiveRecord::Relation#structurally_compatible? Sep 7, 2021 , by Keshav Biswa 2 minute read One of the best thi...
-
4
Rails adds support for Fiber-safe ActiveRecord ConnectionPools Feb 23, 2022 , by Swaathi Kakarla 2 minute read
-
7
rails_7 Published on 15 July 2022
-
3
rails_7 Published on 9 November 2022...
-
5
Ruby ranges are very handy and efficient when you want to work with a sequence of numbers or characters bound by start and end values. But we could come across situations where we want ranges that either have no end value or start value. Ruby 2.6...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK