aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2012-10-02 09:23:02 -0700
committerRafael Mendonça França <rafaelmfranca@gmail.com>2012-10-02 09:23:02 -0700
commit2bc1ac77f688c90fa7e1efc83e60068c64006269 (patch)
tree281ab209e7ad1c580fb919c29ad0ff30f73a6fb7 /activerecord/lib
parent0d41d6956f3dda7e8eb133b623bcfd6ad1eb41a1 (diff)
parent6e56a03f834202af4e57c50a71cc74ea08303bfd (diff)
downloadrails-2bc1ac77f688c90fa7e1efc83e60068c64006269.tar.gz
rails-2bc1ac77f688c90fa7e1efc83e60068c64006269.tar.bz2
rails-2bc1ac77f688c90fa7e1efc83e60068c64006269.zip
Merge pull request #7822 from lulalala/reset-counter-cache-for-has-many-through
Fix reset_counters crashing on has_many :through associations.
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/counter_cache.rb9
1 files changed, 7 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/counter_cache.rb b/activerecord/lib/active_record/counter_cache.rb
index c877079b25..d28cd560d9 100644
--- a/activerecord/lib/active_record/counter_cache.rb
+++ b/activerecord/lib/active_record/counter_cache.rb
@@ -22,8 +22,13 @@ module ActiveRecord
counters.each do |association|
has_many_association = reflect_on_association(association.to_sym)
- foreign_key = has_many_association.foreign_key.to_s
- child_class = has_many_association.klass
+ if has_many_association.is_a? ActiveRecord::Reflection::ThroughReflection
+ foreign_key = has_many_association.through_reflection.foreign_key.to_s
+ child_class = has_many_association.through_reflection.klass
+ else
+ foreign_key = has_many_association.foreign_key.to_s
+ child_class = has_many_association.klass
+ end
belongs_to = child_class.reflect_on_all_associations(:belongs_to)
reflection = belongs_to.find { |e| e.foreign_key.to_s == foreign_key && e.options[:counter_cache].present? }
counter_name = reflection.counter_cache_column