title: Sequel eager load some associations of single ORM object
body: <p>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.</p>
  
  <p>I have read <a href="http://sequel.jeremyevans.net/rdoc/classes/Sequel/Model/Associations/DatasetMethods.html" rel="nofollow">the Sequel::Model::Association</a> guide to the <code>.eager</code> method, but it has confused me a little. A typical example might be:</p>
  
  <pre><code>Artist.eager( :albums =&gt; :tracks ).all
  </code></pre>
  
  <p>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.</p>
  
  <p>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</p>
  
  <pre><code>Artist.eager( :albums =&gt; :tracks ).where( :id =&gt; id ).all.first
  </code></pre>
  
  <p>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.</p>
  
  <p>However, I feel like I have missed something. The construct, having to pass in the primary key to the <code>where</code> clause, get a full dataset then ask for first item seems quite awkward. I am looking for something like this:</p>
  
  <pre><code>Artist.eager( :albums =&gt; :tracks )[ id ]
  </code></pre>
  
  <p>. . . a simple way of declaring I want to load a single object, and eager load some of its associations.</p>
  
  <p>I <em>have</em> found that I can create a custom association like this:</p>
  
  <pre><code>def eager_albums
    albums_dataset.eager( :tracks ).all
  end
  </code></pre>
  
  <p>but that is awkward to use, because the code has to ask for the association in a different way.</p>
  
  <p>My question: Does my construct <code>Artist.eager( :albums =&gt; :tracks ).where( :id =&gt; id ).all.first</code> do what I <em>think</em> it does in Sequel, and could I do better (simpler code)?</p>