Issue: belongs_to with validate: false and optional: false no longer allows saving the child when parent is invalid in Mongoid 9 #6007
Unanswered
lucasDechenier
asked this question in
Q&A
Replies: 2 comments 9 replies
-
Lucas, the situation you're in is understandably painful and frustrating. I hate that we don't have any good workaround for you. To answer your questions:
Again, we really are sorry for the mess. If you'd like to brainstorm about possible workarounds, I'd be happy to participate, but I fear there will be no silver bullet for it in the immediate future. |
Beta Was this translation helpful? Give feedback.
1 reply
-
I will add that the following may work for you, though it is the worst kind of monkey-patching: module WorkAroundBelongsToValidation
private
def setup_instance_methods!
define_getter!
define_setter!
define_existence_check!
define_builder!
define_creator!
define_autosaver!
define_counter_cache_callbacks!
polymorph!
define_dependency!
create_foreign_key_field!
setup_index!
define_touchable!
# The following line changes the conditions under which the association is added; it better copes with
# the otherwise-conflicting `:optional` and `:validate` options, only letting the `:optional` option dominate
# if `:validate` has not been set.
@owner_class.validates_associated(name) if validate? || (require_association? && @options[:validate].nil?)
@owner_class.validates(name, presence: true) if require_association?
end
end
Mongoid::Association::Referenced::BelongsTo.prepend(WorkAroundBelongsToValidation) |
Beta Was this translation helpful? Give feedback.
8 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Context
I'm upgrading an application from Mongoid 7.4.0 to Mongoid 9.0.4, and I encountered a change in how
belongs_to
associations behave when using:In Mongoid 7.4.0, this configuration allowed the child to be persisted, even if the parent was invalid and not persisted, because
validate: false
meant the association wouldn’t be validated.Behavior in Mongoid 7.4.0 (expected and working)
Result:
child
is persisted withtest_parent_id
parent
is not persisted (as expected)child.test_parent
) returnsnil
unless manually savedBehavior in Mongoid 9.0.4 (unexpected regression)
Using the exact same models and test, the behavior changed:
Mongoid refuses to persist the child, even though
validate: false
is explicitly set.Minimal Reproducible Example
Actual Result in Mongoid 9.0.4
child.save
returnsfalse
Additional note
Using
optional: true
across all associations is not a viable workaround in my case, becauseoptional: true
introduces another inconsistency, where assigning an object to the foreign key field (*_id
) causes serialization errors.Because of this, applying optional: true globally across all belongs_to associations in my application caused over 100 test failures, especially in scenarios where foreign keys were being assigned document objects instead of raw IDs.
Additionally, the expectation is that setting validate: false should disable validation entirely for that association—regardless of whether optional is true or false. Requiring both validate: false and optional: true to bypass validation behavior goes against that design intention and causes inconsistencies.
Question
Shouldn't
validate: false
skip validation and allow saving even whenoptional: false
?Is this behavior change intentional?
If so, is there a workaround to restore the previous behavior without rewriting all associations or saving parents manually?
Impact
This change introduces implicit cascade validation behavior that was not present in earlier versions and breaks expectations around
validate: false
. It affected large portions of our codebase where we relied on this to defer validation of associated documents.The
validate: false
is not respectedAny clarification or guidance would be appreciated.
Beta Was this translation helpful? Give feedback.
All reactions