-
-
Notifications
You must be signed in to change notification settings - Fork 196
Updating fixture data counter caches columns
Graeme Porteous edited this page May 5, 2023
·
1 revision
- Create
lib/reset_counter_caches.rb
with the source:
class ResetCounterCaches
def self.reset_all(models = ActiveRecord::Base.descendants)
new(models).reset
end
def initialize(models)
@models = models
end
def reset
models.each do |model|
reset_counter_caches_for(model) unless model.abstract_class?
end
end
private
attr_reader :models
def reset_counter_caches_for(model)
counter_cache_associations(model).each do |association|
reset_for(association)
end
end
def counter_cache_associations(model)
model.reflect_on_all_associations(:belongs_to).select do |association|
association.options[:counter_cache]
end
end
def reset_for(association)
klass = association.klass
column = association.counter_cache_column
klass.find_each { |record| klass.reset_counters(record.id, column) }
end
end
- Add the dumped_railers gem to the development group.
- Reset your development database and re-seed.
- In a Rails development console run:
require 'reset_counter_caches'
ResetCounterCaches.reset_all
PublicBody.find_each { |pb| pb.update_count_caches }
require 'dumped_railers'
DumpedRailers.dump!(User, InfoRequest, PublicBody, base_dir: 'tmp/fixtures/')
- Manually copy
*_count
columns fromtmp/fixtures/*
to the relevant fixtures inspec/fixtures/*