# Problem with before\_\* callbacks being wrapped in transaction

**URL:** <https://www.ruby-forum.com/t/problem-with-before-callbacks-being-wrapped-in-transaction/183195>\
**Category:** Rails\
**Created:** [February 9, 2010, 9:15pm UTC](https://www.ruby-forum.com/t/problem-with-before-callbacks-being-wrapped-in-transaction/183195 "2010-02-09T21:15:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 9, 2010, 9:15pm UTC](https://www.ruby-forum.com/t/problem-with-before-callbacks-being-wrapped-in-transaction/183195/1 "2010-02-09T21:15:43Z")

</div>

I am upgrading a 2.1 Rails app and ran into a problem with the new way  
that before\_\* callbacks are being wrapped in a transaction.

I have a model that has a before\_create callback which charges the  
customers credit card. If the transaction is successful it returns  
true, and if not, false so that the record is not saved. In addition,  
I have a Cclog audit entry that is created regardless if the  
transaction is successful or not. This way I keep an audit trail.

This worked fine and well until somewhere in the 2.2/2.3 version of  
rails. Now the entire before\_create callback is wrapped in a  
transaction which means that my Cclog object gets rolled back if the  
transaction is not successful and returns false. So I cannot keep an  
audit trail of unsuccessful credit card charges.

Is there a way to temporarily disable the transactional nature or the  
callbacks? The only way I can see getting this to work is moving the  
transaction/Cclog creation logic into a charge!() method, and making  
sure to call that method from my controller after it is already saved.

Any suggestions?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 9, 2010, 9:33pm UTC](https://www.ruby-forum.com/t/problem-with-before-callbacks-being-wrapped-in-transaction/183195/2 "2010-02-09T21:33:42Z")

</div>

removed\_email\_address@domain.invalid wrote:

> I am upgrading a 2.1 Rails app and ran into a problem with the new way  
> that before\_\* callbacks are being wrapped in a transaction.
> 
> I have a model that has a before\_create callback which charges the  
> customers credit card. If the transaction is successful it returns  
> true, and if not, false so that the record is not saved. In addition,  
> I have a Cclog audit entry that is created regardless if the  
> transaction is successful or not. This way I keep an audit trail.
> 
> This worked fine and well until somewhere in the 2.2/2.3 version of  
> rails. Now the entire before\_create callback is wrapped in a  
> transaction which means that my Cclog object gets rolled back if the  
> transaction is not successful and returns false. So I cannot keep an  
> audit trail of unsuccessful credit card charges.
> 
> Is there a way to temporarily disable the transactional nature or the  
> callbacks? The only way I can see getting this to work is moving the  
> transaction/Cclog creation logic into a charge!() method, and making  
> sure to call that method from my controller after it is already saved.
> 
> Any suggestions?

Perhaps you could use a savepoint.

But check the credit card companies’ regulations; I seem to recall that  
keeping a log of unsuccessful charges is prohibited…

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 9, 2010, 11:15pm UTC](https://www.ruby-forum.com/t/problem-with-before-callbacks-being-wrapped-in-transaction/183195/3 "2010-02-09T23:15:45Z")

</div>

On Feb 9, 8:14 pm, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> rails. Now the entire before\_create callback is wrapped in a  
> transaction which means that my Cclog object gets rolled back if the  
> transaction is not successful and returns false. So I cannot keep an  
> audit trail of unsuccessful credit card charges.

use save\_without\_transactions ?

Fred

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 10, 2010, 11:01pm UTC](https://www.ruby-forum.com/t/problem-with-before-callbacks-being-wrapped-in-transaction/183195/4 "2010-02-10T23:01:32Z")

</div>

Won’t this savepoint also be rolled back?

Also, I am not storing any credit card numbers, cvv, etc. Basically  
the last 4 digits, decline code, etc.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 10, 2010, 11:28pm UTC](https://www.ruby-forum.com/t/problem-with-before-callbacks-being-wrapped-in-transaction/183195/5 "2010-02-10T23:28:03Z")

</div>

Please quote when replying.

removed\_email\_address@domain.invalid wrote:

> Won’t this savepoint also be rolled back?

Why don’t you try it and find out?

> Also, I am not storing any credit card numbers, cvv, etc. Basically  
> the last 4 digits, decline code, etc.

Check your merchant agreement. I’m no expert, but I’m not sure this is  
allowed.

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
