I actually made a plugin so I can turn this on on a per-model basis where it is important and use a similar technique to what the eager each plugin does albeit simplified!
Thanks for the reply though appreciate your help. -Robert On Thursday, October 10, 2013 12:56:54 AM UTC+13, Robert Payne wrote: > > Hey Jeremy, > > Understand you need all the rows up front for eager loading to perform > properly. Here's my use case for needing it on first as well as traditional > all: > > 1) I have a User object > 2) I have defined a eager_relationships dataset method that eager loads in > the "current_user" relationship to the queried row. This information has > stuff like friends status etc.. > 3) I sometimes pull a single user or a list of users, each situation > sometimes requires the eager loaded relationships > 4) During serialisation to avoid a N+1 problem I check to see if the > relationship associations have been loaded and serialise the relationship > if it has been otherwise I simply leave it off. > > I suppose #4 is where I would need to intervene and explicitly say whether > or not I wanted the relationship included and ensure I eager load properly > beforehand. > > -Robert > > On Monday, October 7, 2013 7:10:26 AM UTC+13, Jeremy Evans wrote: >> >> On Sunday, October 6, 2013 12:16:43 AM UTC-7, Robert Payne wrote: >> >>> I have some eager loading that I need to make happen sometimes on single >>> rows and other times for lists. See the code sample below with some >>> comments but I was curious if there is any (technical) reason why .first >>> and .first! should not perform eager loading or if there is a better work >>> around than using .all.first and then raising the errors myself? >>> >>> class User < Sequel::Model(:users) >>> >>> dataset_module do >>> >>> def eager_relationships >>> # setup some eager loads here >>> end >>> >>> end >>> >>> end >>> >>> User.where(id: [1,2,3]).eager_relationships.all # eager loads >>> User.where(id: 1).eager_relationships.first! # does not eager load >>> >>> User.dataset = User.dataset.clone.eager_relationships # will make first, >>> all, each eager load >>> >> >> You cannot do eager loading without having all rows up front. If you >> really want first/first! to work, you can use the eager_each plugin on the >> model (which forces the use of #all on eager datasets). >> >> Note that there is generally no reason to do eager loading if you are >> only returning a single object. >> >> 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 http://groups.google.com/group/sequel-talk. For more options, visit https://groups.google.com/groups/opt_out.
