# Force an object to previous state, when using lock\_version

**URL:** <https://www.ruby-forum.com/t/force-an-object-to-previous-state-when-using-lock-version/110890>\
**Category:** Rails\
**Created:** [July 31, 2007, 8:49pm UTC](https://www.ruby-forum.com/t/force-an-object-to-previous-state-when-using-lock-version/110890 "2007-07-31T20:49:31Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![helzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/e19b73/32.png) [@helzer](https://www.ruby-forum.com/u/helzer)\
**Post date:** [July 31, 2007, 8:49pm UTC](https://www.ruby-forum.com/t/force-an-object-to-previous-state-when-using-lock-version/110890/1 "2007-07-31T20:49:31Z")

</div>

If I have an exception while trying to save an object, how can I tell  
Rails that it’s not saved yet?

The object has a lock\_version (used for optimistic locking). This is  
what I do:

ins = MyClass.new( arguments )  
begin  
transaction do  
ins.save!  
bunch\_of\_other\_stuff()  
end  
rescue  
ins.save! # problem here!  
end

The object’s save() sets the internal object state in a saved mode. It  
actually isn’t saved because the transaction failed.  
I can’t reload it, because it’s not in the database yet, and all  
subsequent saves are failing because the lock\_version is wrong.

What I currently do, as a work-around is create a new instance of the  
object:  
new\_ins = MyClass.new ( ins.attributes )

Is there any way I can tell Rails that this object was never saved  
before?

Thanks,  
Amir
