Skip to content

Define #preload_relations on Ingredients - #2523

Closed
mamhoff wants to merge 1 commit into
AlchemyCMS:mainfrom
mamhoff:add-preload-relations-to-ingredients
Closed

mamhoff wants to merge 1 commit into
AlchemyCMS:mainfrom
mamhoff:add-preload-relations-to-ingredients

Conversation

@mamhoff

@mamhoff mamhoff commented Jul 19, 2023

Copy link
Copy Markdown
Contributor

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:

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.

Checklist

  • I have followed Pull Request guidelines
  • I have added a detailed description into each commit message
  • I have added tests to cover this change

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
mamhoff requested a review from tvdeyen July 19, 2023 15:35
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 tvdeyen left a comment

Copy link
Copy Markdown
Member

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.

Comment thread app/models/alchemy/ingredient.rb
Comment thread app/models/alchemy/ingredient.rb
@mamhoff

mamhoff commented Jul 20, 2023

Copy link
Copy Markdown
Contributor Author

Closing in favor of AlchemyCMS/alchemy-json_api#72

@mamhoff mamhoff closed this Jul 20, 2023
@mamhoff
mamhoff deleted the add-preload-relations-to-ingredients branch July 20, 2023 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants