diff options
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/transactions.rb | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/transactions.rb b/activerecord/lib/active_record/transactions.rb index 6a776966bb..5ee644c0b4 100644 --- a/activerecord/lib/active_record/transactions.rb +++ b/activerecord/lib/active_record/transactions.rb @@ -126,11 +126,28 @@ module ActiveRecord end def save_with_transactions(perform_validation = true) #:nodoc: - transaction { save_without_transactions(perform_validation) } + rollback_active_record_state { transaction { save_without_transactions(perform_validation) } } end def save_with_transactions! #:nodoc: - transaction { save_without_transactions! } + rollback_active_record_state { transaction { save_without_transactions! } } + end + + # stores the current id and @new_record values so that they are reset + # after rolling the transaction back. + def rollback_active_record_state + previous_new_record = @new_record + previous_id = self.id + response = yield + rescue + response = false + raise + ensure + unless response + @new_record = previous_new_record + self.id = previous_id + end + response end end end |