Skip to content

Commit 4674f42

Browse files
authored
MONGOID-5380 fix assign_attributes on embedded association to work when used before an association assignment (#5340)
1 parent 5f6ff62 commit 4674f42

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

lib/mongoid/association/embedded/batchable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def batch_replace(docs)
113113
batch_remove(_target.dup)
114114
end
115115
elsif _target != docs
116-
_base.delayed_atomic_sets.clear unless _assigning?
116+
_base.delayed_atomic_sets.delete(path) unless _assigning?
117117
docs = normalize_docs(docs).compact
118118
_target.clear and _unscoped.clear
119119
_base.delayed_atomic_unsets.delete(path)

spec/mongoid/association/embedded/embeds_many/proxy_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4835,4 +4835,29 @@ class DNS::Record
48354835
expect(School.count).to eq(0)
48364836
end
48374837
end
4838+
4839+
context "when doing assign_attributes then assignment" do
4840+
4841+
let(:post) do
4842+
EmmPost.create!(
4843+
company_tags: [ EmmCompanyTag.new(title: "1"), EmmCompanyTag.new(title: "1") ],
4844+
user_tags: [ EmmUserTag.new(title: "1"), EmmUserTag.new(title: "1") ]
4845+
)
4846+
end
4847+
4848+
let(:from_db) { EmmPost.find(post.id) }
4849+
4850+
before do
4851+
post.assign_attributes(
4852+
company_tags: [ EmmCompanyTag.new(title: '3'), EmmCompanyTag.new(title: '4') ]
4853+
)
4854+
post.user_tags = [ EmmUserTag.new(title: '3'), EmmUserTag.new(title: '4') ]
4855+
post.save!
4856+
end
4857+
4858+
it "persists the associations correctly" do
4859+
expect(from_db.user_tags.size).to eq(2)
4860+
expect(from_db.company_tags.size).to eq(2)
4861+
end
4862+
end
48384863
end

0 commit comments

Comments
 (0)