On Mon, May 24, 2021 at 11:38 AM [email protected] <[email protected]>
wrote:

> I am dealing a good deal of legacy code that is not very friendly to the
> database with a ton of nested n + 1 calls.  I am slowly replacing them with
> code that takes advantage of eager associations, nested four levels deep,
> which works phenomenally well for this particular use case as each parent
> model instance has a fairly small number of associated rows.
>
> However the logic in the legacy code dynamically creates new rows in first
> association's model (and those continuing three more levels).   When I call
> `refresh`, none one of the nested associations are reloaded.
>

Associations are cleared when refreshing, by design.  Calling the
associations methods will cause the associations to be loaded, but that can
cause N+1 issues.


> I assume this is related to the fact that a refresh is not actually using
> the dataset.all method.
>

No, a refresh explicitly clears the associations hash.  The assumption when
refreshing is that the data in the database has changed, and those changes
could affect the associations that would be loaded.


> One of the other constraining factors I'm facing is that all of this code
> was written at the class level, so I can't simply memoize data with
> instance variables.
>
> Is there any simply way to have refresh or reload eager load all nested
> associations?
>

Currently, there isn't.  You can use the association_lazy_eager_option
plugin to eager load the necessary associations after refreshing.  It may
be possible to build a plugin to automatically eagerly load already loaded
associations when refreshing.

Another possibility is adding an option so that refresh does not clear
associations.

Another possibility that would not require library changes is to call #all
on the dataset you retrieved the records from initially (which performed
the eager_loading), filtered to the ids of the records you want to refresh.
This may be the easiest approach.

Incidentally, if all that weren't enough, we're running a 4.x version of
> Sequel, so I am sure I am missing out on a lot of new features.
>

Probably obvious, but if support for what you want was added, you would
have to upgrade to the latest version of Sequel to get 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 view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/CADGZSSd-ARHcxC3TW-vZ6bi%2BTU-mDNr3TB6_ki2ZRMBPzL%2B0Vw%40mail.gmail.com.

Reply via email to