method

add_counter_cache_callbacks

rails latest stable - Class: ActiveRecord::Associations::Builder::BelongsTo
add_counter_cache_callbacks(model, reflection)
public

No documentation available.

# File activerecord/lib/active_record/associations/builder/belongs_to.rb, line 27
    def self.add_counter_cache_callbacks(model, reflection)
      cache_column = reflection.counter_cache_column

      model.after_update lambda { |record|
        association = association(reflection.name)

        if association.saved_change_to_target?
          association.increment_counters
          association.decrement_counters_before_last_save
        end
      }

      klass = reflection.class_name.safe_constantize
      klass._counter_cache_columns |= [cache_column] if klass && klass.respond_to?(:_counter_cache_columns)
    end