From b1c11d5787fb6081c9cfb04d5e910688a5158e05 Mon Sep 17 00:00:00 2001 From: Bianca Nenciu Date: Wed, 8 Dec 2021 15:23:44 +0200 Subject: [PATCH] FIX: Select correct topic draft for user (#15234) The old query could return multiple rows. --- app/models/user_stat.rb | 2 +- spec/models/draft_spec.rb | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/app/models/user_stat.rb b/app/models/user_stat.rb index 63bac29ec34..7d0093eeadc 100644 --- a/app/models/user_stat.rb +++ b/app/models/user_stat.rb @@ -211,7 +211,7 @@ class UserStat < ActiveRecord::Base UPDATE user_stats SET draft_count = (SELECT COUNT(*) FROM drafts WHERE user_id = :user_id) WHERE user_id = :user_id - RETURNING draft_count, (SELECT 1 FROM drafts WHERE draft_key = :new_topic) + RETURNING draft_count, (SELECT 1 FROM drafts WHERE user_id = :user_id AND draft_key = :new_topic) SQL MessageBus.publish( diff --git a/spec/models/draft_spec.rb b/spec/models/draft_spec.rb index 4dd9de5fe7d..538e9c96c58 100644 --- a/spec/models/draft_spec.rb +++ b/spec/models/draft_spec.rb @@ -179,6 +179,8 @@ describe Draft do end it 'updates draft count when a draft is created or destroyed' do + Draft.set(Fabricate(:user), Draft::NEW_TOPIC, 0, "data") + messages = MessageBus.track_publish("/user") do Draft.set(user, Draft::NEW_TOPIC, 0, "data") end