MONGOID-5751 avoid unnecessary autosaves of unchanged subtrees#6133
Merged
jamis merged 5 commits intomongodb:masterfrom Apr 17, 2026
Merged
MONGOID-5751 avoid unnecessary autosaves of unchanged subtrees#6133jamis merged 5 commits intomongodb:masterfrom
jamis merged 5 commits intomongodb:masterfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses MONGOID-5751 by preventing autosave: true referenced associations from saving (and thus running after_save callbacks on) already-persisted, unchanged documents when a parent save cascades across the association graph.
Changes:
- Add a regression spec that asserts
after_saveis only invoked for the parent and newly-added descendants, not for pre-existing unchanged children. - Update referenced autosave to skip saving unchanged documents while still recursively saving descendants when needed (via a recursive
changed_for_autosave?check).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
spec/mongoid/association/auto_save_spec.rb |
Adds a regression test reproducing the unnecessary autosave/callback behavior for unchanged subtrees. |
lib/mongoid/association/referenced/auto_save.rb |
Adds recursive “needs autosave?” detection and skips saving unchanged referenced documents during autosave traversal. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
alexbevi
approved these changes
Apr 16, 2026
comandeo-mongo
approved these changes
Apr 17, 2026
comandeo-mongo
approved these changes
Apr 17, 2026
jamis
added a commit
to jamis/mongoid
that referenced
this pull request
Apr 17, 2026
…db#6133) * MONGOID-5751 avoid unnecessary autosaves of unchanged subtrees * avoid loading HasMany proxies into memory, just to check if they're dirty * account for belongs_to sometimes storing a literal object instead of a proxy * Add Mongoid.autosave_saves_unchanged_documents feature flag to preserve legacy behavior
jamis
added a commit
that referenced
this pull request
Apr 20, 2026
* MONGOID-5751 avoid unnecessary autosaves of unchanged subtrees (#6133) * MONGOID-5751 avoid unnecessary autosaves of unchanged subtrees * avoid loading HasMany proxies into memory, just to check if they're dirty * account for belongs_to sometimes storing a literal object instead of a proxy * Add Mongoid.autosave_saves_unchanged_documents feature flag to preserve legacy behavior * Update lib/mongoid/config.rb Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes a bug with
autosave: trueassociations, that causedafter_savecallbacks to be invoked on children even if they hadn't changed. It recursively checks the entire subtree and skips saving it if nothing in it has changed.Summary
The legacy behavior of associations with
autosave: trueresulted in all#savebeing invoked on all children of those associations, whether those children actually needed it or not. All correspondingafter_savehooks were invoked as well, recursively, clear to the bottom of the autosave tree.This PR changes this behavior, ensuring that subtrees are only autosaved if there are any changed documents in the subtree. As there may be users that depend on the legacy behavior (expecting
after_savehooks to be invoked even if the record has not changed, for instance), a feature flag has been added to allow the legacy behavior to be opted into.When
true, the legacy behavior prevails. If your program depends on this legacy behavior, you are encouraged to rewrite the affected code, because this flag will go away in Mongoid 10, and the legacy autosave behavior will be removed.