Skip to content

Commit

Permalink
Remove objects from identity map if save! failed, otherwise finding a…
Browse files Browse the repository at this point in the history
…gain the same record will have invalid attributes.
  • Loading branch information
miloops committed Nov 19, 2010
1 parent e83f5a0 commit f3adddb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions activerecord/lib/active_record/attribute_methods/dirty.rb
Expand Up @@ -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.
Expand Down
13 changes: 13 additions & 0 deletions activerecord/test/cases/identity_map_test.rb
Expand Up @@ -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.