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