This blog is part of our Rails 6 series.
Before Rails 6, if we want to extract associated records from an
ActiveRecord::Relation, we would use preload and collect.
For example, we want to fetch subscriptions of some users. The query would
look as shown below.
User.where(blocked: false).preload(:subscriptions).collect(&:subscriptions)
=> # returns collection of subscription records
ActiveRecord::Relation#extract_associated provides a shorthand to achieve same result and is more readable than former.
User.where(blocked: false).extract_associated(:subscriptions)
=> # returns the same collection of subscription records
Here's the relevant pull request for this change.

Written byTaha Husain
Related blogs
- 01Rails 6 adds support to persist timezones of Active JobSeptember 1, 2020 · Chetan Gawai
- 02Rails 6 adds including & excluding method on EnumerablesAugust 18, 2020 · Akhil Gautam
- 03Rails 6 fixes after_commit callback invocation bugFebruary 25, 2020 · Amit Choudhary
- 04Rails 6 adds rails db:prepare to migrate or setup a databaseDecember 10, 2019 · Akhil Gautam