Permalink
Browse files

Remove objects from identity map if save! failed, otherwise finding a…

…gain the same record will have invalid attributes.
  • Loading branch information...
1 parent e83f5a0 commit f3adddb966f3c93de4992b42cf554e3ef2a3acf1 @miloops miloops committed Sep 15, 2010
@@ -33,6 +33,9 @@ def save!(*) #:nodoc:
@previously_changed = changes
@changed_attributes.clear
end
+ rescue
+ IdentityMap.remove(self) if IdentityMap.enabled?
+ raise
end
# <tt>reload</tt> the record and clears changed attributes.
@@ -261,4 +261,17 @@ def test_reload_object_if_save_failed
assert_not_equal developer.salary, same_developer.salary
end
+ def test_reload_object_if_forced_save_failed
+ developer = Developer.first
+ developer.salary = 0
+
+ assert_raise(ActiveRecord::RecordInvalid) { 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

0 comments on commit f3adddb

Please sign in to comment.