Transaction đảm bảo tất cả hoặc không có thao tác DB nào được commit — nếu bất kỳ bước nào fail, toàn bộ rollback.
ActiveRecord::Base.transaction do
sender.update!(balance: sender.balance - amount)
receiver.update!(balance: receiver.balance + amount)
Transfer.create!(from: sender, to: receiver, amount: amount)
end
# Nếu receiver.update! raise → sender.balance cũng bị rollbackTransaction tự động bao quanh mỗi save / create / destroy. Cần explicit transaction khi:
- Nhiều thao tác DB phụ thuộc nhau (chuyển tiền, đặt hàng + trừ kho).
- Dùng save! (raise) để trigger rollback thay vì return false.
Lưu ý: callback after_commit chạy sau khi transaction commit thành công — dùng cho side-effects để không trigger khi rollback.
A transaction guarantees all-or-nothing DB operations — if any step fails, everything is rolled back.
ActiveRecord::Base.transaction do
sender.update!(balance: sender.balance - amount)
receiver.update!(balance: receiver.balance + amount)
Transfer.create!(from: sender, to: receiver, amount: amount)
end
# If receiver.update! raises → sender.balance is also rolled backEvery save / create / destroy is automatically wrapped in a transaction. You need an explicit one when:
- Multiple DB operations depend on each other (money transfers, order + inventory deduction).
- Using save! (raises) to trigger rollback instead of returning false.
Note: the after_commit callback fires only after a successful commit — use it for side-effects to avoid triggering them on rollback.