diff options
author | Mislav Marohnić <mislav.marohnic@gmail.com> | 2010-04-17 02:01:38 +0200 |
---|---|---|
committer | Mikel Lindsaar <raasdnil@gmail.com> | 2010-06-03 23:32:10 +1000 |
commit | e6b0ea3f8a252b6795156ad8c0816198f7c18cf9 (patch) | |
tree | 4994e166c01b660a44e3db476439388b6703b5d2 /activerecord/lib | |
parent | 4ab47f542026f7301a0cf38d7d89ee6423207021 (diff) | |
download | rails-e6b0ea3f8a252b6795156ad8c0816198f7c18cf9.tar.gz rails-e6b0ea3f8a252b6795156ad8c0816198f7c18cf9.tar.bz2 rails-e6b0ea3f8a252b6795156ad8c0816198f7c18cf9.zip |
fix `reset_counters` to work even with complex class names
e.g. it guesses that a belongs_to association to Namespace::MyModel is
named "my_model", unlike before where it would look up an association
named "namespace::mymodel" and fail.
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/counter_cache.rb | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/counter_cache.rb b/activerecord/lib/active_record/counter_cache.rb index 3bdb6970f8..9601ed6afd 100644 --- a/activerecord/lib/active_record/counter_cache.rb +++ b/activerecord/lib/active_record/counter_cache.rb @@ -16,13 +16,15 @@ module ActiveRecord def reset_counters(id, *counters) object = find(id) counters.each do |association| - child_class = reflect_on_association(association).klass - counter_name = child_class.reflect_on_association(self.name.downcase.to_sym).counter_cache_column + child_class = reflect_on_association(association.to_sym).klass + belongs_name = self.name.demodulize.underscore.to_sym + counter_name = child_class.reflect_on_association(belongs_name).counter_cache_column - self.unscoped.where(arel_table[self.primary_key].eq(object.id)).arel.update( + self.unscoped.where(arel_table[self.primary_key].eq(object.id)).arel.update({ arel_table[counter_name] => object.send(association).count - ) + }) end + return true end # A generic "counter updater" implementation, intended primarily to be |