From fb9156f9b8d59da2e55f29f1671d144eed11e4ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Fri, 10 May 2024 02:00:37 +0200 Subject: [PATCH] Use with_lock instead of lock! That way the record is only locked while necessary. --- app/models/budget/ballot/line.rb | 8 ++++---- app/models/poll/answer.rb | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/app/models/budget/ballot/line.rb b/app/models/budget/ballot/line.rb index 8b0849a19..0268ecc74 100644 --- a/app/models/budget/ballot/line.rb +++ b/app/models/budget/ballot/line.rb @@ -18,10 +18,10 @@ class Budget before_validation :set_denormalized_ids def check_enough_resources - ballot.lock! - - unless ballot.enough_resources?(investment) - errors.add(:resources, ballot.not_enough_resources_error) + ballot.with_lock do + unless ballot.enough_resources?(investment) + errors.add(:resources, ballot.not_enough_resources_error) + end end end diff --git a/app/models/poll/answer.rb b/app/models/poll/answer.rb index b99ffcef6..e3fbef6f7 100644 --- a/app/models/poll/answer.rb +++ b/app/models/poll/answer.rb @@ -38,10 +38,10 @@ class Poll::Answer < ApplicationRecord def max_votes return if !question || !author || persisted? - author.lock! - - if question.answers.by_author(author).count >= question.max_votes - errors.add(:answer, "Maximum number of votes per user exceeded") + author.with_lock do + if question.answers.by_author(author).count >= question.max_votes + errors.add(:answer, "Maximum number of votes per user exceeded") + end end end end