Skip to content

Instantly share code, notes, and snippets.

@joost
Forked from svyatov/update_cache_counters.rake
Last active September 25, 2018 15:05
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save joost/7790345 to your computer and use it in GitHub Desktop.
Save joost/7790345 to your computer and use it in GitHub Desktop.
Rails Rake Task: Update all cache counters / counter caches.
# More robust version to update new or existing counter cache columns in your Rails app.
# See: https://gist.github.com/svyatov/4225663
desc 'Update all cache counters'
task :update_cache_counters => :environment do
models_to_update = {}
# or: Rails.application.eager_load!
# Dir loads less, so it's faster
Dir.glob(Rails.root.join('app/models/**/*')).each { |model| require model if File.file?(model) }
# as a convention, cache counter column name must begin with assotiacion's name and end with '_count' suffix,
# e.g. 'comments_count', 'posts_count'
ActiveRecord::Base.descendants.each do |model|
next if not model.table_exists?
cache_counters = model.column_names.select { |n| n =~ /_count\z/ }.map { |n| n.gsub(/_count\z/, '').to_sym }
cache_counters.select! { |c| model.reflect_on_association(c) } # check if association is exists
models_to_update[model] = cache_counters if cache_counters.size > 0
end
models_to_update.each do |model, counters|
model.select(:id).find_each do |record|
model.reset_counters record.id, *counters
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment