-
Type:
Bug
-
Resolution: Fixed
-
Priority:
Unknown
-
Affects Version/s: 9.1.1, 9.0.12
-
Component/s: Autosave, Persistence
-
None
-
None
-
Fully Compatible
-
Ruby Drivers
-
Not Needed
-
-
None
-
None
-
None
-
None
-
None
-
None
A new (unsaved) `embeds_one` document under a persisted parent gets its `embeds_many` association assigned through `assign_attributes`. On save, Mongoid sends two `$set` keys, one for the whole embedded document and one for its child array, and the server rejects the update:
Mongo::Error::OperationFailure: [40]: Updating the path 'group.items' would create a conflict at 'group'
Assigning the same array with the plain setter (`group.items = [...]`) saves correctly.
Failing case:
Given:
class Item include Mongoid::Document field :name, type: String embedded_in :group end class Group include Mongoid::Document field :matcher, type: String embedded_in :parent embeds_many :items end class Parent include Mongoid::Document embeds_one :group, autobuild: true end
Then:
parent = Parent.find(Parent.create!.id) parent.build_group(matcher: 'all') parent.group.assign_attributes(items: [{ name: 'a' }]) parent.save! # raises OperationFailure [40]
Nested attributes into an autobuilt `embeds_one` fail the same way:
parent.assign_attributes(group: { matcher: 'all', items: [{ name: 'a' }] })
parent.save! # raises OperationFailure [40]