# Optimistic Locking and How to Handle?

**URL:** <https://www.ruby-forum.com/t/optimistic-locking-and-how-to-handle/92386>\
**Category:** Rails\
**Created:** [April 18, 2007, 10:56pm UTC](https://www.ruby-forum.com/t/optimistic-locking-and-how-to-handle/92386 "2007-04-18T22:56:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![michael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4bbf92/32.png) [@michael](https://www.ruby-forum.com/u/michael)\
**Post date:** [April 18, 2007, 10:56pm UTC](https://www.ruby-forum.com/t/optimistic-locking-and-how-to-handle/92386/1 "2007-04-18T22:56:30Z")

</div>

Hello,

I need to use the magic column “lock\_version” to prevent users from  
updating a record simultaneously. I’ve got it working without a  
problem.

Has anyone implemented a nice way to handle the conflict resolution. I  
don’t want to just tell the user - Sorry, already been updated, try  
again. Instead, I would like to present a list of fields that are  
conflicted and allow that user who got the error to determine how they  
want to handle the conflict and then save the record.

It seems the way to do something like this would be to query the record  
by id when the StaleObjectError exception is caught. Then, compare the  
params submitted with the freshly queried record and come up with a list  
of values that are different. Take this list, send it back to the user  
and let them decide what to do.

Instead of writing this from scratch my guess is someone on this list  
has already done something similar.

Any pointers?

Thanks,

Michael

---

<div class="post-metadata">

**Author:** ![michael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4bbf92/32.png) [@michael](https://www.ruby-forum.com/u/michael)\
**Post date:** [June 12, 2007, 1:25pm UTC](https://www.ruby-forum.com/t/optimistic-locking-and-how-to-handle/92386/2 "2007-06-12T13:25:45Z")

</div>

Hi,

it’s been some time since your post, but I’ll respond to it anyway  
because  
maybe you’re still looking for a solution.

I’d suggest you to use Riff. It will let you compare two AR objects  
with each other. Before you apply the form data to your object,  
you could clone it to a @dbrecord-var and afterward compare  
both objects with Riff. You can find more on riff here:  
[http://wiki.rubyonrails.org/rails/pages/Riff+Plugin](http://wiki.rubyonrails.org/rails/pages/Riff+Plugin)

Greetings,  
Christoph
