I am building the data model for a small application, and would like to take advantage of eager loading in some methods - i.e. those where I know in advance that certain associations are going to get used.
I have read the Sequel::Model::Association guide to the .eager
method, but it has confused me a little. A typical example might be:
Artist.eager( :albums => :tracks ).all
which loads all the Artist objects with all their albums fields preloaded, and all the tracks pre-loaded, using just three queries. So far, so good.
But say I want to load a single Artist by its primary key, and still have the albums + tracks pre-loaded (still three queries, potentially a lot less than following the associations for each album)? I cannot see any example of that. A little experimentation gives me
Artist.eager( :albums => :tracks ).where( :id => id ).all.first
which seems to at least work. I confirmed the eager loading by calling this, then switching off the db, and showing I could still access the associations.
However, I feel like I have missed something. The construct, having to pass in the primary key to the where
clause, get a full dataset then ask for first item seems quite awkward. I am looking for something like this:
Artist.eager( :albums => :tracks )[ id ]
. . . a simple way of declaring I want to load a single object, and eager load some of its associations.
I have found that I can create a custom association like this:
def eager_albums
albums_dataset.eager( :tracks ).all
end
but that is awkward to use, because the code has to ask for the association in a different way.
My question: Does my construct Artist.eager( :albums => :tracks ).where( :id => id ).all.first
do what I think it does in Sequel, and could I do better (simpler code)?