Skip to content

Commit adfedd7

Browse files
authored
MONGOID-5269 Persist writing an attribute after a removal correctly (#5200)
* MONGOID-5269 Persist writing an attribute after a removal correctly * change to save!
1 parent 52e8020 commit adfedd7

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

lib/mongoid/attributes.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ def write_attribute(name, value)
164164
else
165165
attributes[field_name] = typed_value
166166
end
167+
168+
# when writing an attribute, also remove it from the unsets,
169+
# so that removing then writing doesn't result in a removal.
170+
delayed_atomic_unsets.delete(field_name)
171+
167172
typed_value
168173
end
169174
else

spec/mongoid/attributes_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2214,4 +2214,19 @@
22142214
end
22152215
end
22162216
end
2217+
2218+
context "when an attribute is removed then set" do
2219+
let(:cat) { Cat.create!(name: "Neil") }
2220+
2221+
before do
2222+
cat.remove_attribute("name")
2223+
cat.name = "Nissim"
2224+
cat.save!
2225+
cat.reload
2226+
end
2227+
2228+
it "correctly sets the attribute" do
2229+
expect(cat.name).to eq("Nissim")
2230+
end
2231+
end
22172232
end

0 commit comments

Comments
 (0)