MONGOID-5751 avoid unnecessary autosaves of unchanged subtrees#6134
Merged
jamis merged 2 commits intomongodb:9.0-stablefrom Apr 20, 2026
Merged
MONGOID-5751 avoid unnecessary autosaves of unchanged subtrees#6134jamis merged 2 commits intomongodb:9.0-stablefrom
jamis merged 2 commits intomongodb:9.0-stablefrom
Conversation
…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
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a feature flag to avoid autosaving unchanged referenced-association subtrees (preventing unnecessary after_save callbacks), while preserving legacy behavior behind a configurable option and warning.
Changes:
- Introduces
Mongoid.autosave_saves_unchanged_documentsconfiguration option plus a deprecation-style warning when legacy behavior is in effect. - Updates referenced autosave to skip saving unchanged documents unless they (or in-memory autosaved descendants) are new/changed/marked for destruction.
- Adds a regression spec for MONGOID-5751 verifying
after_savecallbacks do not fire for pre-existing unchanged children when the flag is disabled.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| spec/mongoid/association/auto_save_spec.rb | Adds regression coverage for autosave callback behavior across a parent/child/grandchild referenced tree. |
| lib/mongoid/warnings.rb | Adds a one-time warning for the legacy “save unchanged autosave docs” behavior. |
| lib/mongoid/config.rb | Adds the autosave_saves_unchanged_documents configuration option and documents its intended lifecycle. |
| lib/mongoid/association/referenced/auto_save.rb | Implements recursive “changed in subtree” detection (cycle-guarded) and skips autosaving unchanged docs when configured. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
comandeo-mongo
approved these changes
Apr 20, 2026
This was referenced Apr 23, 2026
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.
(Backport of #6133)
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 adds an option to change this behavior, ensuring that subtrees are only autosaved if there are any changed documents in the subtree.
The default is
true, allowing the legacy behavior to prevail. If your program depends on this legacy behavior, you are encouraged to rewrite the affected code and set the value tofalse. The default value of this option will befalsein Mongoid 9.1, and will go away in Mongoid 10. At that point the legacy autosave behavior will be removed.