Skip to content

MONGOID-5999 Prevent delayed updates from leaking from new embedded children - #6170

Open
ujihisa wants to merge 27 commits into
mongodb:masterfrom
ujihisa:MONGOID-5999
Open

ujihisa wants to merge 27 commits into
mongodb:masterfrom
ujihisa:MONGOID-5999

Conversation

@ujihisa

@ujihisa ujihisa commented Sep 2, 2026

Copy link
Copy Markdown

Description

A persisted embedded document can write a nested association to the wrong place when one attributes= call builds a new embeds_one child.

The new child is built before it is attached to its parent. If it has a nested embeds_many, that association records a delayed atomic set with a path relative to the child. When the root document builds its update, it also includes that delayed set.

With different stored names, the nested array can appear as an unintended field on the root document. With the same stored name, it can be merged into the root array or produce a MongoDB path conflict.

Root cause

EmbedsOne::Buildable#build creates a new child without parent or association metadata. The child therefore cannot calculate a path from the root document.

The embeds_many batch path already supplies that metadata and clears delayed sets for newly collected children. The problem occurs on the embeds_one path.

Fix

Batchable#add_atomic_sets now records delayed atomic sets only when the base document is persisted. A new embedded document is written as part of its parent document, so a delayed set for one of the new child associations is redundant.

Tests

The integration spec uses public attributes=, nested attributes, and save APIs. It covers:

  • Root and nested associations with the same stored name.
  • Root and nested associations with different stored names.
  • Updating, clearing, and leaving the root association unchanged.
  • Updating the nested association on an existing group.
  • Assigning nested attributes to a new embeds_one child.
  • Updating a newly built child after attaching it to the parent.
  • The saved document and the in-memory associations after each update.

Verification

  • All eight regression examples fail without the fix and pass with it.
  • The related unit and integration specs pass: 114 examples, 0 failures, 4 pending.
  • bundle exec rake ci completes every test group with 0 failures.

https://jira.mongodb.org/browse/MONGOID-5999

@ujihisa
ujihisa requested a review from a team as a code owner September 2, 2026 22:27
@ujihisa
ujihisa requested a review from jamis September 2, 2026 22:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant