Flowdock
method

add_counter_cache_callbacks

Importance_0
Ruby on Rails latest stable (v6.1.7.7) - 0 notes - Class: BelongsTo
add_counter_cache_callbacks(model, reflection) public

No documentation

This method has no description. You can help the Ruby on Rails community by adding new notes.

Hide source
# 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.target_changed?
          association.increment_counters
          association.decrement_counters_before_last_save
        end
      }

      klass = reflection.class_name.safe_constantize
      klass.attr_readonly cache_column if klass && klass.respond_to?(:attr_readonly)
    end
Register or log in to add new notes.