Nice, I didn't know the association is still cached when using a block, I think 
we'll go with that.

And we'll consider if perhaps we're much better off using the 
tactical_eager_loading plugin. My current preference is to have data retrieval 
more explicit, and to have one place where we can see everything we're loading. 
The approach you recommended solves that nicely, thank you :)

Kind regards,
Janko
On 8 Feb 2019, 16:09 +0100, Jeremy Evans <[email protected]>, wrote:
> On Friday, February 8, 2019 at 3:32:09 AM UTC-8, Janko Marohnić wrote:
> > I'm looking for an easy way to apply eager loading of any nested 
> > association for a single record, ideally performed at the same time when 
> > the record is retrieved. Currently I can achieve this with:
> >
> >   artist = Artist
> >     .eager(albums: :tracks)
> >     .where(id: artist_id)
> >     .limit(1)
> >     .all
> >     .first
> >
> > However, `Artist.where(id: id).limit(1).all.first` is a lot longer than 
> > just `Artist[id]`. Is there by any chance a shorter way to do it? I'm also 
> > fine with eager loading after the record has been retrieved.
> >
> > My goal is to improve SRP in my app; I want to localize where records are 
> > retrieved, so that when I pass the model instance to a class, that this 
> > class doesn't have to worry about eager loading anymore (one example is a 
> > presenter class which builds a JSON representation which includes fields of 
> > associated records).
>
> There is no reason to eager load if you are retrieving a single record.  It 
> would be better to handle your case via:
>
> artist = Artist[artist_id]
> artist.albums{|ds| ds.eager(:tracks)}
>
> Or you could just use the tactical_eager_loading plugin and not worry about 
> it.
>
> Thanks,
> Jeremy
> --
> You received this message because you are subscribed to a topic in the Google 
> Groups "sequel-talk" group.
> To unsubscribe from this topic, visit 
> https://groups.google.com/d/topic/sequel-talk/z7-CLjWBtFY/unsubscribe.
> To unsubscribe from this group and all its topics, 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.

-- 
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