From e83f5a0ae97889dddfd0956081b0f4582594cda9 Mon Sep 17 00:00:00 2001 From: Emilio Tagua Date: Wed, 15 Sep 2010 11:18:34 -0300 Subject: Remove objects from identity map if save failed, otherwise finding again the same record will have invalid attributes. --- .../lib/active_record/attribute_methods/dirty.rb | 2 ++ activerecord/test/cases/identity_map_test.rb | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/attribute_methods/dirty.rb b/activerecord/lib/active_record/attribute_methods/dirty.rb index 439880c1fa..91d16ca97a 100644 --- a/activerecord/lib/active_record/attribute_methods/dirty.rb +++ b/activerecord/lib/active_record/attribute_methods/dirty.rb @@ -21,6 +21,8 @@ module ActiveRecord if status = super @previously_changed = changes @changed_attributes.clear + elsif IdentityMap.enabled? + IdentityMap.remove(self) end status end diff --git a/activerecord/test/cases/identity_map_test.rb b/activerecord/test/cases/identity_map_test.rb index c51bdf5f69..d4be698082 100644 --- a/activerecord/test/cases/identity_map_test.rb +++ b/activerecord/test/cases/identity_map_test.rb @@ -30,7 +30,7 @@ class IdentityMapTest < ActiveRecord::TestCase end def test_find_id_without_identity_map - IdentityMap.without do + ActiveRecord::IdentityMap.without do assert_not_same(Client.find(3), Client.find(3)) end end @@ -247,4 +247,18 @@ class IdentityMapTest < ActiveRecord::TestCase Developer.joins(', projects').each { |d| assert d.readonly? } Developer.joins(', projects').readonly(false).each { |d| assert d.readonly? } end + + def test_reload_object_if_save_failed + developer = Developer.first + developer.salary = 0 + + assert !developer.save + + same_developer = Developer.first + + assert_not_same developer, same_developer + assert_not_equal 0, same_developer.salary + assert_not_equal developer.salary, same_developer.salary + end + end -- cgit v1.2.3