تخطَّ إلى المحتوى

Missing Transaction For Multi Write (ARCH6065)

هذا المحتوى غير متوفر بلغتك بعد.

Key ruby.orm.missing-transaction-for-multi-write · Severity warning · Category orm

Two or more dependent writes in the same method not wrapped in an explicit transaction/atomic block risk partial commits. If the first write succeeds and the second fails, data is left inconsistent.

def transfer(from, to, amount)
from.update!(balance: from.balance - amount)
to.update!(balance: to.balance + amount) # failure here = inconsistency
end
def transfer(from, to, amount)
ActiveRecord::Base.transaction do
from.update!(balance: from.balance - amount)
to.update!(balance: to.balance + amount)
end
end

Wrap dependent multi-writes in an ActiveRecord::Base.transaction block to make them atomic.