-
Type:
Improvement
-
Resolution: Unresolved
-
Priority:
Unknown
-
None
-
Affects Version/s: 9.0.8
-
Component/s: Validations
-
None
From the #ruby channel in Slack, TSE Taz reported the following. I don't recall why we haven't implemented the validation of embedded documents differently here than that of referenced documents, but I know we've discussed it. Maybe we should discuss it again.
Hi team, I have a customer experiencing an issue with embedded document validation using the Ruby driver. Their model Work includes embeds_many :work_professionals, and they expect saving Work to validate all embedded WorkProfessional objects. This behavior worked in Mongoid v7.5.4 but broke after upgrading to v8.0.8, was fixed in v8.0.9, and then regressed again in v8.0.11.
As I understand it, the root cause relates to the validation logic in associated.rb (located in mongoid/lib/mongoid/validatable)—specifically line 77. In v8.0.8, this line used the condition:
if value && !value.flagged_for_destroy? && (!value.persisted? || value.changed?)In v8.0.9, it was changed to:
if value && !value.flagged_for_destroy?This change (from MONGOID-5822) ensured embedded documents were validated even if they weren’t explicitly modified, provided the parent document changed. However, the adjustment was reverted in v8.0.11 (due to performance regressions identified in
MONGOID-5848), restoring the original behavior where embedded documents are validated only when explicitly changed or newly created.As I understand—and please correct me if I’m mistaken—embedded associations (embeds_many, embedded_in, etc.) currently share the same centralized validation logic as referenced associations (has_many, belongs_to, etc.), even though embedded documents reside inside the parent document. This shared logic explains why WorkProfessional objects aren’t validated unless explicitly changed or newly created.
Do you agree with my understanding of the root cause and the proposed workarounds in the comments of this thread? The customer feels this solution is “sloppy” and unacceptable. Also while they agree that validating childs with belongs_to and has_many relationships is overreach, they feel that embedded documents should be treated differently from referenced associations since they are nested within the parent document—almost like a structured field.
- related to
-
MONGOID-5822 Embedded association validations are broken on updates when they depend on some parent's field
-
- Backlog
-
-
MONGOID-5848 Severe performance degradation in 9.0.5
-
- Closed
-