Forum: Ruby on Rails How to force a lock_version increment

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
javinto (Guest)
on 2008-10-21 15:09
(Received via mailing list)
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
Mark Reginald J. (Guest)
on 2008-10-22 01:59
(Received via mailing list)
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
This topic is locked and can not be replied to.