-
-
Notifications
You must be signed in to change notification settings - Fork 314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Define #preload_relations on Ingredients #2523
Define #preload_relations on Ingredients #2523
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.
This takes the capabilities from AlchemyCMS/alchemy_cms#2523 and allows pre-loading object graphs on ingredient's related objects.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great idea. I think we could also make this a static method.
@@ -90,6 +90,12 @@ def allowed_settings | |||
end | |||
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# 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>] |
@@ -90,6 +90,12 @@ def allowed_settings | |||
end | |||
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. | |||
def preload_relations |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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?
Closing in favor of AlchemyCMS/alchemy-json_api#72 |
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