MONGOID-5934 Fix NoMethodError in nested attributes when raise_not_found_error is false#6156
Merged
comandeo-mongo merged 1 commit intoJun 19, 2026
Conversation
…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.
Contributor
There was a problem hiding this comment.
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
nilguard afterassociation.klass.unscoped.find(...)inNested::Many#update_nested_relation, raisingErrors::DocumentNotFoundwhen the document is missing. - Add RSpec coverage for missing nested ids for both
has_many(with reparenting enabled) andhas_and_belongs_to_many, under bothraise_not_found_errorsettings.
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? |
jamis
approved these changes
Jun 18, 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.
With
Mongoid.raise_not_found_error = false, a nested-attributes hash referencing a non-existent_idon ahas_and_belongs_to_manyassociation (or any association whenallow_reparenting_via_nested_attributesis enabled) crashed withNoMethodError: undefined method 'update_attributes' for nil.Nested::Many#update_nested_relationlooks the document up withassociation.klass.unscoped.find(converted).findhonorsraise_not_found_errorand returnsnilwhen the flag is off, and thenilflowed straight intoupdate_document. The other two not-found branches in the same method raiseErrors::DocumentNotFoundunconditionally, and ActiveRecord likewise raisesRecordNotFoundfor unmatched nested ids regardless of configuration, so this branch now does the same: anilfromfindraisesErrors::DocumentNotFound.Behavior with
raise_not_found_error = trueis unchanged:findraises before the new guard is reached, with the same exception class and message.Changes:
lib/mongoid/association/nested/many.rb: nil guard after thefindin the HABTM/reparenting branch, raisingErrors::DocumentNotFound.spec/mongoid/attributes/nested_spec.rb: new contexts forhas_many(with reparenting enabled) andhas_and_belongs_to_manywith a non-existent id, each coveringraise_not_found_errortrue (regression guard) and false (failed withNoMethodErrorbefore 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 withNoMethodErrorbefore the fix)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 rubocopon changed files — no offensesMONGOID-5934