On Tuesday, February 2, 2016 at 12:54:21 PM UTC-8, Mike Pastore wrote:
>
> Hi Jeremy,
>
> I see you've written in the past that there isn't much use in 
> eager-loading relationships on single models. I have a case where I am 
> retrieving a single object but it has a tree of one-to-many relationships 
> hanging off of it. (I am sideloading related records for a json:api 
> payload.) In this case, it would make sense to eager load the 
> relationships, otherwise the serializer causes Sequel to fire off a query 
> for each element of the tree as it enumerates through the relationships.
>

You can use the :eager association to eager load dependent associations. 
 You can also do this on a per-load basis:

  obj = Model.first
  obj.association{|ds| ds.eager(:dep_association)}

Short of doing something like dataset.eager(foo).where(:id => 
> bar).all.first, is there any way to use Sequel::Dataset#eager with 
> #first/first! and #with_pk/with_pk!? If it can't be added to Sequel, how 
> might I go about writing a plugin to enable this behavior?
>

The example you gave is the best way to handle what you want at present, if 
you don't want to use an approach I detailed above.

If you really want to do this via a plugin, I'm guessing extending the 
eager_each plugin should be possible to handle this case.  I think 
eager_each actually handled it in the past, but some refactoring means it 
no longer works.

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to