instance method
add_counter_cache_callbacks
Ruby on Rails 2.3.18
Since v2.3.18 Last seen in v3.0.20 PrivateAvailable in: v2.3.18 v3.0.20
Signature
add_counter_cache_callbacks(reflection)
No documentation comment.
Parameters
-
reflectionreq
Source
# File activerecord/lib/active_record/associations.rb, line 1361
def add_counter_cache_callbacks(reflection)
cache_column = reflection.counter_cache_column
method_name = "belongs_to_counter_cache_after_create_for_#{reflection.name}".to_sym
define_method(method_name) do
association = send(reflection.name)
association.class.increment_counter(cache_column, association.id) unless association.nil?
end
after_create(method_name)
method_name = "belongs_to_counter_cache_before_destroy_for_#{reflection.name}".to_sym
define_method(method_name) do
association = send(reflection.name)
association.class.decrement_counter(cache_column, association.id) unless association.nil?
end
before_destroy(method_name)
module_eval(
"#{reflection.class_name}.send(:attr_readonly,\"#{cache_column}\".intern) if defined?(#{reflection.class_name}) && #{reflection.class_name}.respond_to?(:attr_readonly)"
)
end
Defined in activerecord/lib/active_record/associations.rb line 1361
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::Associations::ClassMethods