我有一个 ruby on rails 应用程序,有很多 sidekiq 工作人员。一些 worker 可以工作一段时间(至少几分钟)。
当我将这条记录保存在 worker 中时,如何阻止来自其他地方(即 Controller )的更改的某些记录,以避免数据冲突?
最佳答案
您需要锁定模型:
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 .
关于ruby-on-rails - rails 阻止代码中其他地方的更改记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30951396/