Atomic Updates And Performance with ActiveRecord Transactions Discussion
sally.transfer(bob, 10000), it is not rollback!
why?
class Account < ApplicationRecord
validates :balance, numericality:{ greater_than: 0}
def withdraw(amount)
update(balance: balance - amount)
end
def deposit(amount)
update(balance: balance + amount)
end
def transfer(recipient, amount)
transaction do
withdraw(amount)
recipient.deposit(amount)
end
end
end