# module ActiveRecord::CounterCache

# Active Record Counter Cache¶ ↑

### Private Instance Methods

_create_record(*) click to toggle source
Calls superclass method

    # File lib/active_record/counter_cache.rb, line 177
    def _create_record(*)
      id = super

      each_counter_cached_associations do |association|
        if send(association.reflection.name)
          association.increment_counters
          @_after_create_counter_called = true
        end
      end

      id
    end

destroy_row() click to toggle source
Calls superclass method

    # File lib/active_record/counter_cache.rb, line 190
    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
            if send(association.reflection.name)
              association.decrement_counters
            end
          end
        end
      end

      affected_rows
    end

each_counter_cached_associations() { |association(to_sym)| ... } click to toggle source

    # File lib/active_record/counter_cache.rb, line 207
    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