# Transaction scope

**URL:** <https://www.ruby-forum.com/t/transaction-scope/116020>\
**Category:** Rails\
**Created:** [October 1, 2007, 5:57pm UTC](https://www.ruby-forum.com/t/transaction-scope/116020 "2007-10-01T17:57:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [October 1, 2007, 5:57pm UTC](https://www.ruby-forum.com/t/transaction-scope/116020/1 "2007-10-01T17:57:16Z")

</div>

I just read this in the documentation and would like to clarify what it  
means:

Both Base#save and Base#destroy come wrapped in a transaction that  
ensures that whatever you do in validations or callbacks will happen  
under the protected cover of a transaction. So you can use validations  
to check for values that the transaction depend on or you can raise  
exceptions in the callbacks to rollback.

Does this mean that if I do my\_model.save I get a Transaction “for free”  
without having to declare one?

And, if so, does it follow that I can write code in “before\_save” and/or  
“after\_save” that updates a second model, my\_other\_model, safe in the  
knowledge that either both models will update or neither will?

Many thanks.

---

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [October 2, 2007, 11:07am UTC](https://www.ruby-forum.com/t/transaction-scope/116020/2 "2007-10-02T11:07:45Z")

</div>

John L. wrote:

> I just read this in the documentation and would like to clarify what it  
> means:
> 
> Both Base#save and Base#destroy come wrapped in a transaction that  
> ensures that whatever you do in validations or callbacks will happen  
> under the protected cover of a transaction. So you can use validations  
> to check for values that the transaction depend on or you can raise  
> exceptions in the callbacks to rollback.
> 
> Does this mean that if I do my\_model.save I get a Transaction “for free”  
> without having to declare one?
> 
> And, if so, does it follow that I can write code in “before\_save” and/or  
> “after\_save” that updates a second model, my\_other\_model, safe in the  
> knowledge that either both models will update or neither will?

Indeed that is the case. If this behavior is critical to your  
application then I also suggest writing the appropriate test cases  
(which I recommend either way 😉 to ensure this behavior will persist  
over different versions.

–  
Roderick van Domburg  
[http://www.nedforce.com](http://www.nedforce.com)

---

<div class="post-metadata">

**Author:** ![johnlester](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@johnlester](https://www.ruby-forum.com/u/johnlester)\
**Post date:** [October 5, 2007, 3:20pm UTC](https://www.ruby-forum.com/t/transaction-scope/116020/3 "2007-10-05T15:20:51Z")

</div>

> Indeed that is the case.  
> Great, many thanks!

> I also suggest writing the appropriate test cases  
> Yes, you can’t have too many tests!
