Thanks Optimistic Locking facility of Active Record, you can easily implement ACID updates on Ruby level. Here's how to do it:
:lock_version, :integer, default: 0column to model
- Use that column in forms for your model
- Catch and resolve
StaleObjectErroron your model updates:
def add_cash(purse) purse.cash += 100 purse.save! rescue ActiveRecord::StaleObjectError add_cash(purse.reload) end
If you want to be 300% sure, use additionally transactions and locking records on database level (aka Pessimistic Locking).
Bonus: always use InnoDB instead of MyISAM for real business applications. It deals far better with locks and transactions.