On Thursday, October 1, 2015 at 2:57:24 PM UTC-7, Chris Hanks wrote:
>
> There are two things I ran across recently that aren't obviously bugs (so 
> I decided to bring them up here rather than open issues), but do feel like 
> unexpected behavior.
>
> #1 is minor: Refreshing/reloading an instance of a model when it doesn't 
> exist in the DB anymore seems like it should probably raise a 
> Sequel::NoMatchingRow error, rather than what it currently raises, which is 
> a regular old Sequel::Error.
>

I agree that a more specific error would be better, but I think 
NoExistingObject would be better than NoMatchingRow in this case.  I'll 
commit a change for that shortly.
 

> #2 is a bit bigger: Calling #first on a dataset with associations 
> eager-loaded doesn't actually eager load them:
>

This is expected and documented 
(http://sequel.jeremyevans.net/rdoc/classes/Sequel/Model/Associations/DatasetMethods.html#method-i-eager).
 
 You can't eagerly load associated records for all current records unless 
you have all current records upfront, hence the need to use #all instead of 
#each (#first calls #each).

If you are only returning a single record, there is no reason to eager 
load, it wouldn't save you any queries.  If for some reason you still want 
to eagerly load, you can use the eager_each plugin.

Of course, in this simple case it doesn't make much of a difference, but 
> with a more complex set of associations to load it would become 
> significant. In my own case, I'm trying to set up an app to only include 
> associations in serialized JSON if they've been eager-loaded (to have some 
> finer control over what associations are serialized in what responses, and 
> to keep myself honest about eager-loading thoroughly).
>
> This feels very unexpected to me, but I'm not sure if I'm alone, or if 
> changing this behavior would be considered backwards-incompatible.
>
> Feedback/thoughts welcome!
>

I wouldn't try to introspect Model#associations to determine what 
associations to include in the JSON.  In general, you should just pick the 
associations, and if they aren't already loaded for the object, they will 
be eagerly loaded.  If you are worried about doing that for a set objects, 
make sure the related dataset uses eager and calls #all.

You may also want to look at the tactical_eager_loading plugin.

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/d/optout.

Reply via email to