Skip to content

MONGOID-5935 Fixed UnknownAttribute error with nested attributes#6155

Open
jamis wants to merge 1 commit into
mongodb:masterfrom
jamis:5935-nested-attributes-with-destroy
Open

MONGOID-5935 Fixed UnknownAttribute error with nested attributes#6155
jamis wants to merge 1 commit into
mongodb:masterfrom
jamis:5935-nested-attributes-with-destroy

Conversation

@jamis
Copy link
Copy Markdown
Contributor

@jamis jamis commented May 29, 2026

This fixes the raised Mongoid::Errors::UnknownAttribute error when nested attributes include _destroy: true for a child whose _id is not already in the parent's relation (e.g. a new parent receiving an existing child's _id).

Copilot AI review requested due to automatic review settings May 29, 2026 17:31
@jamis jamis requested a review from a team as a code owner May 29, 2026 17:31
@jamis jamis requested a review from comandeo-mongo May 29, 2026 17:31
Copy link
Copy Markdown
Contributor

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

Fixes Mongoid::Errors::UnknownAttribute raised during nested-attributes processing when _destroy is provided for an existing child document that is not currently part of the parent’s relation (notably when reparenting via nested attributes is enabled).

Changes:

  • Update nested many-association handling to skip reparenting/update logic when the incoming nested attributes are destroy requests (_destroy), preventing _destroy from being passed into update_attributes.
  • Add regression specs for both has_many and has_and_belongs_to_many relations ensuring _destroy on a non-related existing document does not raise and does not add the document to the relation.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
lib/mongoid/association/nested/many.rb Prevents _destroy payloads from flowing into reparenting/update paths for non-related existing documents.
spec/mongoid/attributes/nested_spec.rb Adds coverage for destroy-on-non-related-document cases for has_many and HABTM nested attributes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

2 participants