diff options
author | José Valim <jose.valim@gmail.com> | 2010-07-08 23:45:10 +0200 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-07-08 23:45:10 +0200 |
commit | d9ebc76d70a122544452189f0cb93d3854f2043f (patch) | |
tree | 35bdea4dfbbd59510792b0f3a483d918bbed8c4c /activerecord/lib | |
parent | 786342e17f42799ef889cf6127fe97e9598272e0 (diff) | |
download | rails-d9ebc76d70a122544452189f0cb93d3854f2043f.tar.gz rails-d9ebc76d70a122544452189f0cb93d3854f2043f.tar.bz2 rails-d9ebc76d70a122544452189f0cb93d3854f2043f.zip |
Refactor previous commits a bit.
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/counter_cache.rb | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/activerecord/lib/active_record/counter_cache.rb b/activerecord/lib/active_record/counter_cache.rb index 808e70d7de..237cd56683 100644 --- a/activerecord/lib/active_record/counter_cache.rb +++ b/activerecord/lib/active_record/counter_cache.rb @@ -18,14 +18,17 @@ module ActiveRecord object = find(id) counters.each do |association| 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) + + expected_name = if has_many_association.options[:as] + has_many_association.options[:as].to_s.classify + else + self.name end - belongs_name = belongs_to_association.name - counter_name = child_class.reflect_on_association(belongs_name).counter_cache_column + + 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 } + counter_name = reflection.counter_cache_column self.unscoped.where(arel_table[self.primary_key].eq(object.id)).arel.update({ arel_table[counter_name] => object.send(association).count |