aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/counter_cache.rb
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 15:58:49 -0300
commit3f0bc97912482c2da9e70683b029feb36de74c27 (patch)
tree68c9326af7dbba14f3978f2075ded7ad4ea33ade /activerecord/lib/active_record/counter_cache.rb
parent2bad605873b5b720d77ae6388a995827ab7fe705 (diff)
downloadrails-3f0bc97912482c2da9e70683b029feb36de74c27.tar.gz
rails-3f0bc97912482c2da9e70683b029feb36de74c27.tar.bz2
rails-3f0bc97912482c2da9e70683b029feb36de74c27.zip
Merge pull request #7822 from lulalala/reset-counter-cache-for-has-many-through
Fix reset_counters crashing on has_many :through associations. Conflicts: activerecord/CHANGELOG.md activerecord/lib/active_record/counter_cache.rb
Diffstat (limited to 'activerecord/lib/active_record/counter_cache.rb')
-rw-r--r--activerecord/lib/active_record/counter_cache.rb10
1 files changed, 8 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/counter_cache.rb b/activerecord/lib/active_record/counter_cache.rb
index 0ba54e6248..7d9b1b45b3 100644
--- a/activerecord/lib/active_record/counter_cache.rb
+++ b/activerecord/lib/active_record/counter_cache.rb
@@ -25,8 +25,14 @@ module ActiveRecord
self.name
end
- 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