Optimistic Locking in Rails not allowing record to be modified in the console

248 Views Asked by At

I'm working on the database locking to ensure the safe data transactions in a multi-user participation.

The machine is installed with Passenger.

After I added the 'lock_version'(integer), the record no long is able to be modified through console.

Let's say,

lesson = Lesson.find(1)
lesson.start_time = Time.now
lesson.save 

=> then, ActiveRecord::StaleObjectError: Attempted to update a stale object: Lesson

But, with a very rare chance, a column is saved, which makes me confused a lot.

The rails version is 4.2.8

Many thanks over many thoughts..!

Best

1

There are 1 best solutions below

0
Sungpah Lee On

SO it turns out that in order to use Optimistic Locking, I should've added lock_version with the initial value 0 (integer), not null. Although in Rails console record shows 0, but in the database it actually was null. !