Breaking Change After Upgrading to Mongoid 9.0.6 – belongs_to_required_by_default = false Doesn't Restore Legacy Behavior #6006
Replies: 0 comments
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.
-
Hi,
I recently upgraded a Rails app from Mongoid 7.4 to Mongoid 9.0.6 and encountered unexpected behavior related to the
belongs_to_required_by_default
setting.Context
In Mongoid 7.4 passing a full document object to an
*_id
field worked without issue:Mongoid would automatically extract the ID from the object and assign it to the foreign key field.
What I did after upgrading
To try to restore the legacy behavior from Mongoid 7.4, I set:
Many of my tests started failing.
The Problem
Even with this configuration, I’m now getting the following error when passing a document object into the foreign key field:
Error:
Expected Behavior (Mongoid 7.4)
In Mongoid 7.4, Mongoid would handle this gracefully, extracting the
.id
from the object and saving it as the reference.Minimal Reproducible Example
Expected result (Mongoid 7.4):
TrackEmail
is created withcompany_id
set tocompany.id
.Current result (Mongoid 9.0.6):
Raises
BSON::Error::UnserializableClass
.Hypothesis
It appears that in recent versions of Mongoid, passing a document object directly to an
*_id
field is no longer supported whenoptional: true
is declared, or whenbelongs_to_required_by_default
is set to false. Instead, Mongoid attempts to serialize the object as-is, causing a failure.This may be related to stricter internal validation or BSON serialization changes.
Impact
create
,update
, andfind_or_create_by
calls are affected because the object was passed into*_id
fields instead of the rawid
Questions
*_id
fields?Thanks in advance for your time and support.
Beta Was this translation helpful? Give feedback.
All reactions