# How to force a lock\_version increment

**URL:** <https://www.ruby-forum.com/t/how-to-force-a-lock-version-increment/151436>\
**Category:** Rails\
**Created:** [October 21, 2008, 1:09pm UTC](https://www.ruby-forum.com/t/how-to-force-a-lock-version-increment/151436 "2008-10-21T13:09:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![javinto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e79b87/32.png) [@javinto](https://www.ruby-forum.com/u/javinto)\
**Post date:** [October 21, 2008, 1:09pm UTC](https://www.ruby-forum.com/t/how-to-force-a-lock-version-increment/151436/1 "2008-10-21T13:09:37Z")

</div>

Hi everyone,

I have a order with order\_lines use case. On changing the lines, I  
want to be sure no one else changed the order. So I use lock\_version  
on the order. This works fine if something is changed on the order  
record.

But now i have the situation that I delete, alter and add order lines  
without changing the order record itself. I have to check and  
increment the order#lock\_version however in advance. If I do this:

@order.update\_attributes!( {:lock\_version=\> params[:order]  
[:lock\_version]} )

nothing gets changed nor with:

@order.update\_attributes!( params[:order] )

as the attributes itself haven’t changed. I could solve it with a  
dummy column that I could change to current time or so but that’s just  
not a clear solution.

Any suggestions?

Thanks

Jan

---

<div class="post-metadata">

**Author:** ![javinto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e79b87/32.png) [@javinto](https://www.ruby-forum.com/u/javinto)\
**Post date:** [October 21, 2008, 11:59pm UTC](https://www.ruby-forum.com/t/how-to-force-a-lock-version-increment/151436/2 "2008-10-21T23:59:54Z")

</div>

javinto wrote:

> Any suggestions?

Perhaps pessimistic locking would be better.

If you find the order record using the :lock =\> true option  
(inside a transaction block!) you prevent any other handler  
from even reading the order record while you’re working on it.

Or you can eager load both the order and its order\_lines in  
a single find with either an exclusive or a shared lock. This  
would lock each of these records in the database until a record  
is updated or the transaction ends.

–  
Rails Wheels - Find Plugins, List & Sell Plugins -  
[http://railswheels.com](http://railswheels.com)
