How to force a lock_version increment


#1

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


#2

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