Skip to content

Commit bf28262

Browse files
authored
MONGOID-5206 fix bug where embedded document is not re-embedded (#5115)
* MONGOID-5206 fix bug where embedded document is not re-embedded * MONGOID-5206 update test description * MONGOID-5206 add comment explaining change
1 parent 994f2b9 commit bf28262

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

lib/mongoid/association/embedded/embeds_one/proxy.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,29 @@ def substitute(replacement)
5353
# The associated object will be replaced by the below update if non-nil, so only
5454
# run the callbacks and state-changing code by passing persist: false in that case.
5555
_target.destroy(persist: !replacement) if persistable?
56+
57+
# A little explanation on why this is needed... Say we have three assignments:
58+
#
59+
# canvas.palette = palette
60+
# canvas.palette = nil
61+
# canvas.palette = palette
62+
# Where canvas embeds_one palette.
63+
#
64+
# Previously, what was happening was, on the first assignment,
65+
# palette was considered a "new record" (new_record?=true) and
66+
# thus palette was being inserted into the database. However,
67+
# on the third assignment, we're trying to reassign the palette,
68+
# palette is no longer considered a new record, because it had
69+
# been inserted previously. This is not exactly accurate,
70+
# because the second assignment ultimately removed the palette
71+
# from the database, so it needs to be reinserted. Since the
72+
# palette's new_record is false, Mongoid ends up "updating" the
73+
# document, which doesn't reinsert it into the database.
74+
#
75+
# The change I introduce here, respecifies palette as a "new
76+
# record" when it gets removed from the database, so if it is
77+
# reassigned, it will be reinserted into the database.
78+
_target.new_record = true
5679
end
5780
unbind_one
5881
return nil unless replacement

spec/mongoid/association/accessors_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,19 @@
166166
end
167167
end
168168
end
169+
170+
context "when the association is set to nil first" do
171+
172+
let!(:name) do
173+
person.build_name
174+
end
175+
176+
it "returns true" do
177+
person.name = nil
178+
person.name = name
179+
expect(person).to have_name
180+
end
181+
end
169182
end
170183

171184
context "when the association is an embeds many" do

spec/mongoid/reloadable_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,29 @@
288288
end
289289
end
290290

291+
context "when embedded documents are unasssigned and reassigned" do
292+
293+
let(:palette) do
294+
Palette.new
295+
end
296+
297+
let(:canvas) do
298+
Canvas.create!
299+
end
300+
301+
before do
302+
canvas.palette = palette
303+
canvas.palette = nil
304+
canvas.palette = palette
305+
canvas.save!
306+
canvas.reload
307+
end
308+
309+
it "reloads the embedded document correctly" do
310+
expect(canvas.palette).to eq(palette)
311+
end
312+
end
313+
291314
context "with relational associations" do
292315

293316
let(:person) do

0 commit comments

Comments
 (0)