How to lock multiple resources in RoR
Jul 27, 2024
When I tried to lock multiple records at one time, I noticed that the doc of Rails didn’t notice it.
accounts = Account.where(...)
account1 = accounts.detect { |account| ... }
account2 = accounts.detect { |account| ... }
I wanted to lock detected accounts without getting each one. However, I managed to do that in the project I’m belonging to.
How can we lock multiple records?
ActiveRecord::Base.transaction do
locked_accounts = Account.where(
id: ids,
).lock
locked_accounts.save!
end
end
We can do like this.