diff options
author | Neeraj Singh <neerajdotname@gmail.com> | 2010-06-27 11:17:44 -0400 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-07-08 23:24:12 +0200 |
commit | 1e53404fe9c39ad0849894d73e431315be8c0bf0 (patch) | |
tree | 57f1bc98e37e8f5ed17ed8126d1b43d3c6578288 /activerecord/lib/active_record/counter_cache.rb | |
parent | 87f64ef05e48ad8b374022ca944e34a7ad68551d (diff) | |
download | rails-1e53404fe9c39ad0849894d73e431315be8c0bf0.tar.gz rails-1e53404fe9c39ad0849894d73e431315be8c0bf0.tar.bz2 rails-1e53404fe9c39ad0849894d73e431315be8c0bf0.zip |
reset_counter should work with non-traditional belongs_to and polymorphic belongs_to
[#4984 state:resolved]
Signed-off-by: José Valim <jose.valim@gmail.com>
Diffstat (limited to 'activerecord/lib/active_record/counter_cache.rb')
-rw-r--r-- | activerecord/lib/active_record/counter_cache.rb | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/counter_cache.rb b/activerecord/lib/active_record/counter_cache.rb index 999322129a..808e70d7de 100644 --- a/activerecord/lib/active_record/counter_cache.rb +++ b/activerecord/lib/active_record/counter_cache.rb @@ -17,8 +17,14 @@ module ActiveRecord def reset_counters(id, *counters) object = find(id) counters.each do |association| - child_class = reflect_on_association(association.to_sym).klass - belongs_name = self.name.demodulize.underscore.to_sym + has_many_association = reflect_on_association(association.to_sym) + polymorphic_class = has_many_association.options[:as] + child_class = has_many_association.klass + belongs_to = child_class.reflect_on_all_associations(:belongs_to) + belongs_to_association = belongs_to.detect do |e| + polymorphic_class.nil? ? (e.class_name == self.name) : (e.class_name.to_s.downcase == polymorphic_class.to_s.downcase) + end + belongs_name = belongs_to_association.name 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({ @@ -103,4 +109,4 @@ module ActiveRecord update_counters(id, counter_name => -1) end end -end
\ No newline at end of file +end |