Skip to content

Instantly share code, notes, and snippets.

@rebelweb
Created October 17, 2019 11:42
Show Gist options
  • Save rebelweb/8e10ad612186fbe202170e3da14c6162 to your computer and use it in GitHub Desktop.
Save rebelweb/8e10ad612186fbe202170e3da14c6162 to your computer and use it in GitHub Desktop.
Update Counter Cache Job
class UpdateCounterCacheJob < ApplicationJob attr_accessor :cache_columns
def perform(key = 'all')
self.cache_columns = counter_cache_hash
if key == 'all'
cache_columns.keys.each { |obj| update_cache_columns(obj) }
else
update_cache_columns(key)
end
end
private
def counter_cache_hash
file_name = format('%s/config/counter_cache_columns.json', Rails.root)
file = File.read(file_name)
JSON.parse(file)
end
def update_cache_columns(key)
model = key.camelize.constantize.base_class
cache_columns[key].each do |col|
model.find_each { |m| model.reset_counters(m.id, col) }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment