rails 阻止记录更改代码中的其他地方

rails block a record for change for another places in the code

我在 rails 上有一个 ruby 应用程序,有很多 sidekiq 工作人员。一些工人可以工作一段时间(至少几分钟)。

如何阻止来自其他地方(即控制器)的更改的某些记录,以避免在工作程序中保存此记录时发生数据冲突?

您需要锁定模型:

account = Account.first

account.with_lock do
  # This block is called within a transaction,
  # account is already locked.
  account.balance -= 100
  account.save!
end

您可以阅读更多相关信息 here