Define #preload_relations on Ingredients#2523
Closed
mamhoff wants to merge 1 commit intoAlchemyCMS:mainfrom
Closed
Define #preload_relations on Ingredients#2523mamhoff wants to merge 1 commit intoAlchemyCMS:mainfrom
mamhoff wants to merge 1 commit intoAlchemyCMS:mainfrom
Conversation
In order to be able to preload ingredients' related object graphs, we
need to be able to tell the rails preloader how those graphs look.
This adds a method that can be used on for example an element like this:
```
def preload_ingredient_relations(element)
element.ingredients.group_by(&:preload_relations).each do |preload_relations, ingredients|
preload(records: ingredients.map(&:related_object).compact, associations: preload_relations)
end
element
end
def preload(records:, associations:)
if Rails::VERSION::MAJOR >= 7
ActiveRecord::Associations::Preloader.new(records: records, associations: associations).call
else
ActiveRecord::Associations::Preloader.new.preload(records, associations)
end
end
```
Most notorious for n+1 problems is the picture ingredient that tends to
have a lot of thumbs that we actually need.
mamhoff
added a commit
to AlchemyCMS/alchemy-json_api
that referenced
this pull request
Jul 19, 2023
This takes the capabilities from AlchemyCMS/alchemy_cms#2523 and allows pre-loading object graphs on ingredient's related objects.
tvdeyen
reviewed
Jul 20, 2023
Member
tvdeyen
left a comment
There was a problem hiding this comment.
Great idea. I think we could also make this a static method.
| end | ||
|
|
||
| # This method can be overwritten in individual ingredients to fetch objects that belong to the related object in some form. | ||
| # This takes an Array of things that can be passed to the Rails preloader. |
Member
There was a problem hiding this comment.
Suggested change
| # This takes an Array of things that can be passed to the Rails preloader. | |
| # This takes an Array of relation names that can be passed to the Rails preloader. | |
| # @return [Array<Symbol>] |
|
|
||
| # This method can be overwritten in individual ingredients to fetch objects that belong to the related object in some form. | ||
| # This takes an Array of things that can be passed to the Rails preloader. | ||
| def preload_relations |
Member
There was a problem hiding this comment.
Wondering if this could be a static method of some kind, as this does not need the instance? I know it is read on the instance, but it could as well be the class instead, right?
Contributor
Author
|
Closing in favor of AlchemyCMS/alchemy-json_api#72 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What is this pull request for?
This allows code that renders pages or elements with ingredients to preload the object graphs below ingredient's related objects.
Notable changes (remove if none)
In order to be able to preload ingredients' related object graphs, we need to be able to tell the rails preloader how those graphs look.
This adds a method that can be used on for example an element like this:
Most notorious for n+1 problems is the picture ingredient that tends to have a lot of thumbs that we actually need.
Checklist