Skip to content

Commit c76082f

Browse files
authored
MONGOID-5935 Fixed UnknownAttribute error with nested attributes (#6155)
1 parent 0a04a76 commit c76082f

2 files changed

Lines changed: 51 additions & 5 deletions

File tree

lib/mongoid/association/nested/many.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,14 @@ def update_nested_relation(parent, id, attrs)
192192
elsif association.embedded?
193193
raise Errors::DocumentNotFound.new(association.klass, id)
194194
elsif association.is_a?(Association::Referenced::HasAndBelongsToMany) || Mongoid.allow_reparenting_via_nested_attributes?
195-
Mongoid::Warnings.warn_reparenting_via_nested_attributes if Mongoid.allow_reparenting_via_nested_attributes?
195+
unless destroyable?(attrs)
196+
Mongoid::Warnings.warn_reparenting_via_nested_attributes if Mongoid.allow_reparenting_via_nested_attributes?
196197

197-
# push existing document to association
198-
doc = association.klass.unscoped.find(converted)
199-
update_document(doc, attrs)
200-
existing.push(doc) unless destroyable?(attrs)
198+
# push existing document to association
199+
doc = association.klass.unscoped.find(converted)
200+
update_document(doc, attrs)
201+
existing.push(doc)
202+
end
201203
else
202204
raise Errors::DocumentNotFound.new(association.klass, { _id: id, association.foreign_key => parent.id })
203205
end

spec/mongoid/attributes/nested_spec.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,29 @@
189189
expect(person2.posts.map(&:title)).to eq([ 'Reparented!' ])
190190
end
191191
end
192+
193+
context 'when _destroy is true for a document not in the relation' do
194+
config_override :allow_reparenting_via_nested_attributes, true
195+
196+
before do
197+
Person.send(:undef_method, :posts_attributes=)
198+
Person.accepts_nested_attributes_for :posts, allow_destroy: true
199+
end
200+
201+
let(:person3) do
202+
Person.create!(
203+
posts_attributes: { '0' => { id: post.id, _destroy: '1' } }
204+
)
205+
end
206+
207+
it 'does not raise UnknownAttribute' do
208+
expect { person3 }.not_to raise_error
209+
end
210+
211+
it 'does not add the document to the relation' do
212+
expect(person3.posts).to be_empty
213+
end
214+
end
192215
end
193216
end
194217

@@ -225,6 +248,27 @@
225248
)
226249
expect(person.preferences.map(&:name)).to eq([ preference_name ])
227250
end
251+
252+
context 'when _destroy is true for a document not in the relation' do
253+
before do
254+
Person.send(:undef_method, :preferences_attributes=)
255+
Person.accepts_nested_attributes_for :preferences, allow_destroy: true
256+
end
257+
258+
let(:person) do
259+
Person.new(
260+
preferences_attributes: { 0 => { id: preference.id, _destroy: '1' } }
261+
)
262+
end
263+
264+
it 'does not raise UnknownAttribute' do
265+
expect { person }.not_to raise_error
266+
end
267+
268+
it 'does not add the document to the relation' do
269+
expect(person.preferences).to be_empty
270+
end
271+
end
228272
end
229273
end
230274

0 commit comments

Comments
 (0)