class

Ralph::EagerLoading::Preloader

Inherits Reference / Object

Preloader - handles the separate-query preloading strategy

This is the recommended strategy for most use cases. It executes N+1 queries as 2 queries using IN clause batching.

Example:

authors = Author.all
Preloader.preload(authors, :posts)
# Now authors.each { |a| a.posts } won't trigger additional queries

Class methods

preload(models : Array(T), includes : IncludeSpec) : Nil forall T

Preload associations on a collection of models

Supports various include specifications:

  • Symbol: preload(:posts)
  • Array: preload([:posts, :comments])
  • Hash: preload({posts: :comments})
Source
preload_association(models : Array(T), association : Symbol) : Nil forall T

Preload a single association on all models

Source