aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorDavid Peter <david.a.peter@gmail.com>2012-01-16 18:06:14 -0800
committerDavid Peter <david.a.peter@gmail.com>2012-01-16 19:06:45 -0800
commitee013a503db125b8be54daec7096e2d26bb6228c (patch)
treeccb096e2f9fe053ffe1538b9276c6632973abda8 /activerecord/lib/active_record
parent21afd9b96d70d1e2b1cffdfb60f7ec64ab240472 (diff)
downloadrails-ee013a503db125b8be54daec7096e2d26bb6228c.tar.gz
rails-ee013a503db125b8be54daec7096e2d26bb6228c.tar.bz2
rails-ee013a503db125b8be54daec7096e2d26bb6228c.zip
Fix bug where reset_counters resets the wrong counter cache.
If a model belongs_to two associations with the same class, then reset_counters will reset the wrong counter cache. Finding the right reflection should use the foreign_key instead, which should be unique.
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/counter_cache.rb3
1 files changed, 2 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/counter_cache.rb b/activerecord/lib/active_record/counter_cache.rb
index 031918712a..c9c46b8d4f 100644
--- a/activerecord/lib/active_record/counter_cache.rb
+++ b/activerecord/lib/active_record/counter_cache.rb
@@ -25,9 +25,10 @@ module ActiveRecord
self.name
end
+ foreign_key = has_many_association.foreign_key.to_s
child_class = has_many_association.klass
belongs_to = child_class.reflect_on_all_associations(:belongs_to)
- reflection = belongs_to.find { |e| e.class_name == expected_name }
+ reflection = belongs_to.find { |e| e.foreign_key.to_s == foreign_key }
counter_name = reflection.counter_cache_column
stmt = unscoped.where(arel_table[primary_key].eq(object.id)).arel.compile_update({