gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 18:12:56 25 4
gpt4 key购买 nike

我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com