-
Type:
Bug
-
Resolution: Unresolved
-
Priority:
Major - P3
-
None
-
Affects Version/s: None
-
Component/s: Associations, Persistence, Validations
-
None
The current implementation of nested attributes is not safe. This is because with has_many associations, the nested transaction builder will:
1. silently ignore validation failures on child objects
2. immediately save child objects (even if parent objects ultimately fail to save)
Ultimately, there is no way to maintain complete parity with AR on this feature, because of MongoDB's lack of cross-collection bulk transactions in server versions earlier than 8, and also because MongoDB lacks nested transactions (or even save points, which can be used to implement nested transactions). MongoDB's transaction model gives a session a single transaction, which must succeed or fail at one shot.
One possible solution is to create a changeset manager that all writes must pass through. It will aggregate all creates, updates, and deletes, and will only write them (in bulk) once it is explicitly asked to do so. This would only be atomic in server versions 8+ (with cross-collection bulk writes).
Note that this solution also has issues, because it is not unheard of for validations and before_save hooks to peek at the database to see if a related object exists in some expected state. With the changeset approach, that kind of inspection will no longer work, because data would not be persisted at all until the entire changeset is committed. Still, it's probably the best we're going to get without save points.
- blocks
-
MONGOID-5905 9.0.8 fix for MONGOID-5888 not complete - validation bypassed in creations
-
- Blocked
-
-
MONGOID-5906 Using assign_attributes with nested attributes on has_many model erroneously persists
-
- Blocked
-