Rails locking across multiple forms
2009-11-24 by , tagged as
In a rails application i needed to lock an object across multiple forms.
When i tried to increment lock_version the locking code prevented it.
To use opportunistic locking across multiple forms i tried to increment the lock_version column in the actions which modify the underlying infomations. In this way i can prevent updating stale data from the other forms.
I got the "Attempted to update a stale object" error because the locking mechanism itself detected the update of lock_version.
The trick is to have a "dummy" column and to update this one instead. In this way lock_version is incremented automatically and prevents the update in all forms which have the corresponding lock_version field.