Rails 6 has addedActiveRecord::Relation#extract_associated for extracting associated records from a relation.
Before
Trying to access collections from related collections on objects,
is a common use case in many applications.
A simple example is if we want to fetch
all authors of posts
from a given category.
Previously, if we wanted to extract such associated records
from a relation
we would make use of preload and collect.
This usage of preload(:author).collect(&:author) can get repetitive
and does not describe the intent of usage properly.
extract_associated method
Relation#extract_associated has now been introduced as a shorthand for preload and collect.
It extracts a named association from the relation.
The named association is first preloaded,
then the individual association records are collected from the relation.
extract_associated returns collection of associated collections in case of belongs_to and has_many associations.