Skip to content

Conversation

jamis
Copy link
Contributor

@jamis jamis commented Aug 20, 2025

Backport to 9.0-stable

With #6012 (MONGOID-5848, reverting a change that caused a performance regression), validation failures of deeply nested children (i.e. grandchildren and deeper) no longer interrupt the transaction for saving a parent. This PR addresses that by adding a new internal flag (children_may_have_changed) that the changed API now uses to indicate whether a document should validate it's children or not.

The value of that flag is set to true when a child document is processed via nested attributes.

…ongodb#6028)

* MONGOID-5888 make sure nested attributes trigger validations on deeply nested children

* code review feedback
@Copilot Copilot AI review requested due to automatic review settings August 20, 2025 21:02
@jamis jamis requested a review from a team as a code owner August 20, 2025 21:02
@jamis jamis requested a review from comandeo-mongo August 20, 2025 21:02
@jamis jamis added the bug Fixes a bug, with no new features or broken compatibility label Aug 20, 2025
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes validation of deeply nested children in Mongoid associations by adding a new internal flag (children_may_have_changed) to ensure that validation failures in grandchildren and deeper nested documents correctly interrupt parent document transactions.

  • Introduces a new children_may_have_changed! method and internal flag to track when nested attributes processing occurs
  • Updates the children_changed? method to consider the new flag in addition to existing change detection
  • Modifies nested attribute builders to set the flag when processing nested attributes

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
spec/mongoid/association/referenced/has_one_models.rb Updates test models to use polymorphic associations and adds nested attribute support with validation
spec/mongoid/association/referenced/has_many_models.rb Adds new test models for has_many associations with nested attributes and validation
spec/integration/associations/has_one_spec.rb Adds integration tests for deeply nested validation scenarios in has_one associations
spec/integration/associations/has_many_spec.rb Adds integration tests for deeply nested validation scenarios in has_many associations
spec/integration/associations/has_and_belongs_to_many_spec.rb Adds test models and integration tests for deeply nested validation in HABTM associations
spec/integration/associations/embeds_many_spec.rb Adds test models and integration tests for deeply nested validation in embeds_many associations
lib/mongoid/changeable.rb Implements the core logic with new flag and methods to track when children may have changed
lib/mongoid/association/nested/one.rb Updates one-to-one nested attribute builder to set the children_may_have_changed flag
lib/mongoid/association/nested/many.rb Updates one-to-many nested attribute builder to set the children_may_have_changed flag

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@jamis jamis merged commit 40f93d2 into mongodb:9.0-stable Aug 21, 2025
62 checks passed
@jamis jamis deleted the 5888-grandchild-validations-9.0-stable branch August 21, 2025 16:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Fixes a bug, with no new features or broken compatibility
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants