Missing Transaction For Multi Write (ARCH6065)
Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.
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.
Incorrect
Section titled “Incorrect”def transfer(from, to, amount) from.update!(balance: from.balance - amount) to.update!(balance: to.balance + amount) # failure here = inconsistencyendCorrect
Section titled “Correct”def transfer(from, to, amount) ActiveRecord::Base.transaction do from.update!(balance: from.balance - amount) to.update!(balance: to.balance + amount) endendWrap dependent multi-writes in an ActiveRecord::Base.transaction block to make them atomic.