favorites: fix users with incorrect favorite counts.

Fix users that have a non-zero favorite count, but no favorites.
Follow-up to 69facb22b.
This commit is contained in:
evazion
2022-01-08 14:17:33 -06:00
parent 69facb22b1
commit 04d3a04836

View File

@@ -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