class Counters::RepairMemberSnipsCountJob < ApplicationJob
queue_as :maintenance
def perform(member_id)
member = Member.find(member_id)
count = Snip.where(author_id: member.id).count
member.update!(snips_count: count)
end
end
module Counters
class EnqueueRepair
def call
Member.find_in_batches(batch_size: 1000) do |batch|
batch.each { |m| Counters::RepairMemberSnipsCountJob.perform_later(m.id) }
end
end
end
end
Counter caches drift (deleted records, backfills, manual SQL). A repair job that recomputes counts safely is invaluable. It’s the kind of operational code you’re glad you wrote the first time a dashboard is wrong.