Skip to content

MONGOID-5934 Fix NoMethodError in nested attributes when raise_not_found_error is false#6156

Merged
comandeo-mongo merged 1 commit into
mongodb:masterfrom
comandeo-mongo:MONGOID-5934-nested-attributes-not-found
Jun 19, 2026
Merged

MONGOID-5934 Fix NoMethodError in nested attributes when raise_not_found_error is false#6156
comandeo-mongo merged 1 commit into
mongodb:masterfrom
comandeo-mongo:MONGOID-5934-nested-attributes-not-found

Conversation

@comandeo-mongo

Copy link
Copy Markdown
Contributor

With Mongoid.raise_not_found_error = false, a nested-attributes hash referencing a non-existent _id on a has_and_belongs_to_many association (or any association when allow_reparenting_via_nested_attributes is enabled) crashed with NoMethodError: undefined method 'update_attributes' for nil.

Nested::Many#update_nested_relation looks the document up with association.klass.unscoped.find(converted). find honors raise_not_found_error and returns nil when the flag is off, and the nil flowed straight into update_document. The other two not-found branches in the same method raise Errors::DocumentNotFound unconditionally, and ActiveRecord likewise raises RecordNotFound for unmatched nested ids regardless of configuration, so this branch now does the same: a nil from find raises Errors::DocumentNotFound.

Behavior with raise_not_found_error = true is unchanged: find raises before the new guard is reached, with the same exception class and message.

Changes:

  • lib/mongoid/association/nested/many.rb: nil guard after the find in the HABTM/reparenting branch, raising Errors::DocumentNotFound.
  • spec/mongoid/attributes/nested_spec.rb: new contexts for has_many (with reparenting enabled) and has_and_belongs_to_many with a non-existent id, each covering raise_not_found_error true (regression guard) and false (failed with NoMethodError before the fix).

Test plan (all against a local 3-node replica set):

  • spec/mongoid/attributes/nested_spec.rb — 713 examples, 0 failures (new examples fail with NoMethodError before the fix)
  • Regression sweep: spec/mongoid/attributes, spec/mongoid/attributes_spec.rb, spec/mongoid/association/nested, spec/mongoid/association/referenced/has_and_belongs_to_many, spec/mongoid/association/referenced/has_many, spec/mongoid/association/embedded, spec/mongoid/persistable — 6028 examples, 0 failures, 13 pending (pre-existing)
  • bundle exec rubocop on changed files — no offenses

MONGOID-5934

…und_error is false

When raise_not_found_error is false, find returns nil instead of
raising, and Nested::Many#update_nested_relation passed the nil to
update_document, crashing with NoMethodError. Raise DocumentNotFound
instead, consistent with the other not-found branches in the same
method.
@comandeo-mongo comandeo-mongo added the bug Fixes a bug, with no new features or broken compatibility label Jun 10, 2026
@comandeo-mongo comandeo-mongo marked this pull request as ready for review June 18, 2026 08:58
@comandeo-mongo comandeo-mongo requested a review from a team as a code owner June 18, 2026 08:58
@comandeo-mongo comandeo-mongo requested review from Copilot and jamis June 18, 2026 08:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Fixes a crash in nested-attributes processing when Mongoid.raise_not_found_error = false, by ensuring a missing related document in the HABTM / reparenting code path raises Mongoid::Errors::DocumentNotFound rather than flowing nil into update_document.

Changes:

  • Add a nil guard after association.klass.unscoped.find(...) in Nested::Many#update_nested_relation, raising Errors::DocumentNotFound when the document is missing.
  • Add RSpec coverage for missing nested ids for both has_many (with reparenting enabled) and has_and_belongs_to_many, under both raise_not_found_error settings.

Reviewed changes

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

File Description
lib/mongoid/association/nested/many.rb Adds a guard to raise DocumentNotFound when find returns nil in the HABTM/reparenting branch.
spec/mongoid/attributes/nested_spec.rb Adds regression specs covering missing nested ids with raise_not_found_error both true and false.

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

# Mongoid.raise_not_found_error is false; a missing document
# must still be an error here, consistent with the other
# not-found branches.
raise Errors::DocumentNotFound.new(association.klass, id) if doc.nil?
@comandeo-mongo comandeo-mongo merged commit 25cfe21 into mongodb:master Jun 19, 2026
76 checks passed
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.

3 participants