From 04d3a048362ad425bab44d2f11b3f55abb124f2f Mon Sep 17 00:00:00 2001 From: evazion Date: Sat, 8 Jan 2022 14:17:33 -0600 Subject: [PATCH] favorites: fix users with incorrect favorite counts. Fix users that have a non-zero favorite count, but no favorites. Follow-up to 69facb22b. --- app/models/user.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/models/user.rb b/app/models/user.rb index 94df37b02..806f69bef 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -602,6 +602,10 @@ class User < ApplicationRecord class_methods do def fix_favorite_counts! + # Fix users that have a non-zero favorite_count but no favorites. + User.where("favorite_count != 0").where.not(id: Favorite.select(:user_id).distinct).update_all(favorite_count: 0) + + # Fix users that have a favorite_count inconsistent with the favorites table. User.find_by_sql(<<~SQL.squish) UPDATE users SET favorite_count = true_count