Skip to content

Commit 4dcdadd

Browse files
MONGOID-5899 Write conflict error when accessing embedded documents via [] operator after direct DB update (#6077)
* MONGOID-5899 Write conflict error when accessing embedded documents via [] operator after direct DB update (#6076) * Adds support for single hash input in nested attributes for has_many associations. * Fixing write conflict error when using [] to access embedded document * undoing accidental changes * Trigger workflows
1 parent a300511 commit 4dcdadd

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

lib/mongoid/attributes.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ def read_attribute(name)
104104
# @api private
105105
def process_raw_attribute(name, raw, field)
106106
value = field ? field.demongoize(raw) : raw
107-
attribute_will_change!(name) if value.resizable?
107+
is_relation = relations.key?(name)
108+
attribute_will_change!(name) if value.resizable? && !is_relation
108109
value
109110
end
110111

spec/mongoid/attributes_spec.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2722,7 +2722,23 @@
27222722
end
27232723
end
27242724

2725-
context "when modifiying a set referenced with the [] notation" do
2725+
context "when accessing an embedded document with the attribute accessor" do
2726+
let(:band) { Band.create! }
2727+
2728+
before do
2729+
Band.where(id: band.id).update_all({
2730+
:$push => {records: { _id: BSON::ObjectId.new }}
2731+
})
2732+
end
2733+
2734+
it "does not throw a conflicting update error" do
2735+
b1 = Band.find(band.id)
2736+
b1[:records].is_a?(Array).should be true
2737+
expect { b1.save! }.not_to raise_error
2738+
end
2739+
end
2740+
2741+
context "when modifying a set referenced with the [] notation" do
27262742
let(:catalog) { Catalog.create!(set_field: [ 1 ].to_set) }
27272743

27282744
before do

0 commit comments

Comments
 (0)