module ActiveRecord::CounterCache

def _create_record(attribute_names = self.attribute_names)

def _create_record(attribute_names = self.attribute_names)
  id = super
  each_counter_cached_associations do |association|
    association.increment_counters
  end
  id
end

def destroy_row

def destroy_row
  affected_rows = super
  if affected_rows > 0
    each_counter_cached_associations do |association|
      foreign_key = association.reflection.foreign_key.to_sym
      unless destroyed_by_association && destroyed_by_association.foreign_key.to_sym == foreign_key
        association.decrement_counters
      end
    end
  end
  affected_rows
end

def each_counter_cached_associations

def each_counter_cached_associations
  _reflections.each do |name, reflection|
    yield association(name.to_sym) if reflection.belongs_to? && reflection.counter_cache_column
  end
end