# Transaction blocks and error handling

**URL:** <https://www.ruby-forum.com/t/transaction-blocks-and-error-handling/178818>\
**Category:** Rails\
**Created:** [November 11, 2009, 7:36pm UTC](https://www.ruby-forum.com/t/transaction-blocks-and-error-handling/178818 "2009-11-11T19:36:18Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![SurviveStyle5](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/eb9ed0/32.png) [@SurviveStyle5](https://www.ruby-forum.com/u/SurviveStyle5)\
**Post date:** [November 11, 2009, 7:36pm UTC](https://www.ruby-forum.com/t/transaction-blocks-and-error-handling/178818/1 "2009-11-11T19:36:18Z")

</div>

I have a question about transaction blocks and error handling.

I have an big\_object that has an array of other\_objects.

So i’m updating these objects from user input and i wrap the whole  
thing in a transaction, go through each of the objects verify they are  
valid and then save them if they are if they aren’t rollback  
transaction and jump back to the page and let the user correct their  
mistake.

#something like…  
ActiveRecord::Base.transaction do  
#update big\_object  
for other\_objects in big\_object do |other\_object|  
#update other\_object  
unless other\_object.valid?  
#oops! looks like my input validations are bad, abort!  
raise ActiveRecord::Rollback  
end  
end  
big\_object.save!  
end

So when i roll back, i throw it back to the page and give the user  
some feed back and a chance to correct their mistake. The problem is  
the object is in a bad state here, it thinks it is saved. So the  
second time my update function is run it goes through says “hey this  
time everything is awesome, lets save” and nothing gets saved to the  
database.  
I know transactions don’t rollback the @objects that are inside of  
them, any suggestions for got error handling in this situation?
