Rails 7 added support for eager loading all rich text associations at once. We can now use #with_all_rich_text instead of eager loading each rich text association separately with #with_rich_text_{field_name}.

### Before

We could eager load rich text associations using the helpers provided by ActionText.

Let’s take an example of the Post model. We have 3 rich text fields in the Post model - :summary, :body and :tldr.

Now, if we have to eager load all the rich_text associations we would end up doing the following:

This will still make 4 queries to the database.

• 1 query to load the Post and
• 3 queries to the ActionText table to load the 3 rich_texts associations.

We can see the same in the log below.

### After

The new method #with_all_rich_text allows to load all the rich text associations at once.

With the same Post model with three action_text associations.

We will now use the newly introduced method #with_all_rich_text.

The above method will now only fire one query to the database. This is how the query will be generated.

How this works?

The following method is responsible for finding all the rich_text associations for the model.

It is collecting all the has_one associations for the model starting with rich_text_ and eager loading all the associations at once.