Skip to content

Commit 5997f6c

Browse files
MONGOID-5874 Fix persisting embedded children (#5988)
1 parent 177d351 commit 5997f6c

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ def initialize(base, target, association)
3636
bind_one
3737
characterize_one(_target)
3838
update_attributes_hash(_target)
39-
_base._reset_memoized_descendants!
4039
_target.save if persistable?
40+
_base._reset_memoized_descendants!
4141
end
4242
end
4343

spec/integration/associations/embeds_one_spec.rb

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
# frozen_string_literal: true
2-
# rubocop:todo all
32

43
require 'spec_helper'
54

65
describe 'embeds_one associations' do
7-
8-
context 're-associating the same object' do
6+
context 'when re-associating the same object' do
97
context 'with dependent: destroy' do
108
let(:canvas) do
119
Canvas.create!(palette: Palette.new)
@@ -17,7 +15,7 @@
1715
canvas.palette = canvas.palette
1816
canvas.save!
1917
canvas.reload
20-
canvas.palette.should == palette
18+
expect(canvas.palette).to eq palette
2119
end
2220
end
2321
end
@@ -31,12 +29,33 @@
3129
end
3230

3331
it 'loads the association correctly' do
34-
expect { klass }.to_not raise_error
35-
expect { klass.new.address }.to_not raise_error
32+
expect { klass }.not_to raise_error
33+
expect { klass.new.address }.not_to raise_error
3634
instance = klass.new
3735
address = Address.new
3836
instance.address = address
3937
expect(instance.address).to eq address
4038
end
4139
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
4261
end

0 commit comments

Comments
 (0)