[Rails] When to use optimistic locking?

Peter De Berdt
2006-05-09T16:44:44Z

> What if I want to guard against 2 people simultaneously updating
> the same customer record? How would I go about doing this? I can''t
> find any examples online, and I''m a bit lost.

Well, first of all, you need a lock_version column in your model
(table) (or override it with the "set_locking_column" method). If a
record has been changed by someone else, Rails will throw a
StaleObjectError, you need to trap this error and handle it
appropriately (present the user with a dialog confirming to update
the record anyway or cancel it). It''s up to you to find a way to
handle the error.

Best regards

Peter De Berdt