module ActiveRecord::CounterCache - activerecord-4.2.3 Documentation

# module ActiveRecord::CounterCache

# Active Record Counter Cache¶ ↑

### Protected Instance Methods

actually_destroyed?() click to toggle source

    # File lib/active_record/counter_cache.rb, line 128
    def actually_destroyed?
      @_actually_destroyed
    end

clear_destroy_state() click to toggle source

    # File lib/active_record/counter_cache.rb, line 132
    def clear_destroy_state
      @_actually_destroyed = nil
    end

### Private Instance Methods

_create_record(*) click to toggle source
Calls superclass method

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