---
title: "Rails 6 adds ActiveRecord::Relation#extract_associated"
description:
  "Rails 6 added ActiveRecord::Relation#extract_associated for extracting
  associated records from a relation"
canonical_url: "https://www.bigbinary.com/blog/rails-6-adds-activerecord-relation-extract_associated"
markdown_url: "https://www.bigbinary.com/blog/rails-6-adds-activerecord-relation-extract_associated.md"
---

# Rails 6 adds ActiveRecord::Relation#extract_associated

Rails 6 added ActiveRecord::Relation#extract_associated for extracting
associated records from a relation

- Author: Taha Husain
- Published: April 17, 2019
- Categories: Rails 6, Rails

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.

#### Rails 5.2

```ruby
User.where(blocked: false).preload(:subscriptions).collect(&:subscriptions)

=> # returns collection of subscription records
```

[ActiveRecord::Relation#extract_associated](https://github.com/rails/rails/pull/35784)
provides a shorthand to achieve same result and is more readable than former.

#### Rails 6.0.0.beta3

```ruby
User.where(blocked: false).extract_associated(:subscriptions)

=> # returns the same collection of subscription records
```

Here's the relevant [pull request](https://github.com/rails/rails/pull/35784)
for this change.

## Links

- [Human page](https://www.bigbinary.com/blog/rails-6-adds-activerecord-relation-extract_associated)
