Module: Sequel::Plugins::EagerGraphEager
- Defined in:
- lib/sequel/plugins/eager_graph_eager.rb
Overview
The eager_graph_eager plugin allows for chaining eager loads after eager_graph loads. Given the following model associations:
Band.one_to_many :albums
Album.one_to_many :tracks
Let’s say you wanted to return bands ordered by album name, and eagerly load those albums, you can do that using:
Band.eager_graph(:albums).order{albums[:name]}
Let’s say you also wanted to eagerly load the tracks for each album. You could just add them to the eager_graph call:
Band.eager_graph(albums: :tracks).order{albums[:name]}
However, the bloats the result set, and you aren’t ordering by the track information, so a join is not required. The eager_graph_eager plugin allows you to specify that the tracks be eagerly loaded in a separate query after the eager_graph load of albums:
Band.eager_graph(:albums).eager_graph_eager([:albums], :tracks).order{albums[:name]}
Dataset#eager_graph_eager
‘s first argument is a dependency chain, specified as an array of symbols. This specifies the point at which to perform the eager load. The remaining arguments are arguments that could be passed to Dataset#eager to specify what dependent associations should be loaded at that point.
If you also have the following model association:
Track.one_to_many :lyrics
Here’s some different ways of performing eager loading:
# 4 Queries: bands, albums, tracks, lyrics
Band.eager(albums: {tracks: :lyrics})
# 1 Query: bands+albums+tracks+lyrics
Band.eager_graph(albums: {tracks: :lyrics})
# 3 Queries: bands+albums, tracks, lyrics
Band.eager_graph(:albums).eager_graph_eager([:albums], tracks: :lyrics)
# 2 Queries: bands+albums+tracks, lyrics
Band.eager_graph(albums: :tracks).eager_graph_eager([:albums, :tracks], :lyrics)
# 2 Queries: bands+albums, tracks+lyrics
Band.eager_graph(:albums).eager_graph_eager([:albums], tracks: proc{|ds| ds.eager_graph(:lyrics)})
Usage:
# Support eager_graph_eager in all subclass datasets (called before loading subclasses)
Sequel::Model.plugin :eager_graph_eager
# Support eager_graph_eager in Album class datasets
Album.plugin :eager_graph_eager
Defined Under Namespace
Modules: DatasetMethods