Rails: 如何锁定 ActiveRecords 以避免数据传输中的冲突?

Rails: How to lock ActiveRecords in order to avoid collisions in data trafic?

我们如何在允许另一个线程或请求对 ActiveRecord 进行更改之前锁定 ActiveRecord 并完成对其的操作?

I am building an eCommerce site which currently has a huge discount campaign. Only one unit of your product left. And many people want to buy it at the exact same time. In such scenario, I need to lock the product?

在 Rails 中,您可以使用 with_lock

https://apidock.com/rails/ActiveRecord/Locking/Pessimistic/with_lock