diff options
author | Rick Olson <technoweenie@gmail.com> | 2007-01-02 05:36:30 +0000 |
---|---|---|
committer | Rick Olson <technoweenie@gmail.com> | 2007-01-02 05:36:30 +0000 |
commit | 1af2022cc32b604529a5ac3a85a3bd92a3c42936 (patch) | |
tree | 494c54bce8f7446f066afea2675425233342fdfa /activerecord/lib | |
parent | b75f28edb4675250e5f18cf9760e4a11a5f4c926 (diff) | |
download | rails-1af2022cc32b604529a5ac3a85a3bd92a3c42936.tar.gz rails-1af2022cc32b604529a5ac3a85a3bd92a3c42936.tar.bz2 rails-1af2022cc32b604529a5ac3a85a3bd92a3c42936.zip |
Rollback #new_record? and #id values for created records that rollback in an after_save callback. Closes #6910 [Ben Curren]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5830 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib')
-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 |