Skip to content

Commit c7773eb

Browse files
committed
MONGOID-5841 don't auto-build if the parent is frozen
1 parent 8eb4727 commit c7773eb

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

lib/mongoid/association/accessors.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ def self.define_getter!(association)
304304
association.inverse_class.tap do |klass|
305305
klass.re_define_method(name) do |reload = false|
306306
value = get_relation(name, association, nil, reload)
307-
if value.nil? && association.autobuilding? && !without_autobuild?
307+
if !frozen? && value.nil? && association.autobuilding? && !without_autobuild?
308308
value = send("build_#{name}")
309309
end
310310
value

spec/mongoid/association/accessors_spec.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
describe Mongoid::Association::Accessors do
77

8-
describe "\#{getter}?" do
8+
describe '#{getter}?' do
99

1010
let(:person) do
1111
Person.create!
@@ -242,7 +242,7 @@
242242
end
243243
end
244244

245-
describe "\#{getter}" do
245+
describe '#{getter}' do
246246

247247
let(:person) do
248248
Person.new
@@ -267,6 +267,16 @@
267267
end
268268
end
269269

270+
context 'when the parent is frozen' do
271+
before do
272+
person.freeze
273+
end
274+
275+
it 'returns nil' do
276+
expect(person.passport).to be_nil
277+
end
278+
end
279+
270280
context "when the association exists" do
271281

272282
let!(:passport) do

0 commit comments

Comments
 (0)