Skip to content

Commit c2b3d4a

Browse files
MONGOID-5874 Fix persisting embedded children (mongodb#5988)
1 parent e34c6cc commit c2b3d4a

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ def initialize(base, target, association)
3232
bind_one
3333
characterize_one(_target)
3434
update_attributes_hash(_target)
35-
_base._reset_memoized_descendants!
3635
_target.save if persistable?
36+
_base._reset_memoized_descendants!
3737
end
3838
end
3939

spec/integration/associations/embeds_one_spec.rb

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
require 'spec_helper'
44

55
describe 'embeds_one associations' do
6-
7-
context 're-associating the same object' do
6+
context 'when re-associating the same object' do
87
context 'with dependent: destroy' do
98
let(:canvas) do
109
Canvas.create!(palette: Palette.new)
@@ -16,7 +15,7 @@
1615
canvas.palette = canvas.palette
1716
canvas.save!
1817
canvas.reload
19-
canvas.palette.should == palette
18+
expect(canvas.palette).to eq palette
2019
end
2120
end
2221
end
@@ -30,12 +29,33 @@
3029
end
3130

3231
it 'loads the association correctly' do
33-
expect { klass }.to_not raise_error
34-
expect { klass.new.address }.to_not raise_error
32+
expect { klass }.not_to raise_error
33+
expect { klass.new.address }.not_to raise_error
3534
instance = klass.new
3635
address = Address.new
3736
instance.address = address
3837
expect(instance.address).to eq address
3938
end
4039
end
40+
41+
context 'when parent is persisted' do
42+
let!(:person) do
43+
Person.create!
44+
end
45+
46+
context 'when assigning the new child' do
47+
context 'when assigning an attribute to the child' do
48+
before do
49+
# person.reload
50+
person.name = Name.new
51+
person.name.first_name = 'Dmitry'
52+
person.save!
53+
end
54+
55+
it 'persists the child' do
56+
expect(person.reload.name.first_name).to eq 'Dmitry'
57+
end
58+
end
59+
end
60+
end
4161
end

0 commit comments

Comments
 (0)