From f0a5936091adb753e7aa8b90621dbe11defca152 Mon Sep 17 00:00:00 2001 From: evazion Date: Tue, 8 Mar 2022 03:15:16 -0600 Subject: [PATCH] Fix #5035: Double flags being created in certain conditions. Lock the post to prevent a race condition when validating that the user hasn't already flagged the post. --- app/models/post_flag.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/models/post_flag.rb b/app/models/post_flag.rb index b46acb631..977274c1d 100644 --- a/app/models/post_flag.rb +++ b/app/models/post_flag.rb @@ -8,6 +8,8 @@ class PostFlag < ApplicationRecord belongs_to :creator, class_name: "User" belongs_to :post + + before_validation { post.lock! } validates :reason, presence: true, length: { in: 1..140 } validate :validate_creator_is_not_limited, on: :create validate :validate_post, on: :create