Skip to content

Commit e910ec6

Browse files
authored
Merge pull request rails#46090 from nvasilevski/do-not-write-to-store-unchanged-value
Do not write unchanged value to ActiveRecord::Store
2 parents 859af30 + 53082b7 commit e910ec6

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

activerecord/lib/active_record/store.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def self.read(object, attribute, key)
225225

226226
def self.write(object, attribute, key, value)
227227
prepare(object, attribute)
228-
object.public_send(attribute)[key] = value
228+
object.public_send(attribute)[key] = value if value != read(object, attribute, key)
229229
end
230230

231231
def self.prepare(object, attribute)

activerecord/test/cases/store_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ class StoreTest < ActiveRecord::TestCase
3131
assert_equal "37signals.com", @john.homepage
3232
end
3333

34+
test "writing store attributes does not update unchanged value" do
35+
admin_user = Admin::User.new(homepage: nil)
36+
admin_user.homepage = nil
37+
assert_equal({}, admin_user.settings)
38+
end
39+
3440
test "reading store attributes through accessors with prefix" do
3541
assert_equal "Quinn", @john.parent_name
3642
assert_nil @john.parent_birthday

0 commit comments

Comments
 (0)