Concurrency Database Security in ROR

Trying to figure out how concurrency is viewed in Ruby On Rails.

How to get a code segment to lock rows in the database and force rollback if necessary?

In particular, is there a way to force a certain segment of code to end and, if not rollback? . I want to add the transaction history to my project, and I do not want transactions to commit without saving the history, so if the server is between two actions (saving the transaction and saving the history), the database may go into an illegal state.

+5
source share
2 answers

ActiveRecord .

Account.transaction do
  account = Account.find(account_id)
  account.lock!

  if account.balance >= 100
    account.balance -= 100
    account.save
  end
end
+5

, Rails. :

YourModel.transaction do
  rec1.save!
  rec2.save!
end

+1

All Articles