Method: ActiveRecord::Reflection::AbstractReflection#inverse_which_updates_counter_cache
- Defined in:
- activerecord/lib/active_record/reflection.rb
#inverse_which_updates_counter_cache ⇒ Object Also known as: inverse_updates_counter_cache?
We need to avoid the following situation:
* An associated record is deleted via record.destroy
* Hence the callbacks run, and they find a belongs_to on the record with a
:counter_cache which points back at our owner. So they update the
counter cache.
* In which case, we must make sure to *not* update the counter cache, or else
it will be decremented twice.
Hence this method.
285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'activerecord/lib/active_record/reflection.rb', line 285 def inverse_which_updates_counter_cache unless @inverse_which_updates_counter_cache_defined if counter_cache_column inverse_candidates = inverse_of ? [inverse_of] : klass.reflect_on_all_associations(:belongs_to) @inverse_which_updates_counter_cache = inverse_candidates.find do |inverse| inverse.counter_cache_column == counter_cache_column && (inverse.polymorphic? || inverse.klass == active_record) end end @inverse_which_updates_counter_cache_defined = true end @inverse_which_updates_counter_cache end |