diff options
Diffstat (limited to 'activerecord')
3 files changed, 14 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/associations/belongs_to_association.rb b/activerecord/lib/active_record/associations/belongs_to_association.rb index 6b29e3ef92..e4758f4369 100644 --- a/activerecord/lib/active_record/associations/belongs_to_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_association.rb @@ -46,7 +46,7 @@ module ActiveRecord if counter_cache_name && @owner.persisted? && different_target?(record) if record - target_klass.increment_counter(counter_cache_name, record.id) + record.class.increment_counter(counter_cache_name, record.id) end if foreign_key_present diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb index 6dcbbc7e34..f97f89b6fe 100644 --- a/activerecord/test/cases/associations/belongs_to_associations_test.rb +++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb @@ -567,4 +567,16 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase client.client_of = companies(:first_firm).id assert_equal companies(:first_firm), firm.reload end + + def test_polymorphic_counter_cache + tagging = taggings(:welcome_general) + post = posts(:welcome) + comment = comments(:greetings) + + assert_difference 'post.reload.taggings_count', -1 do + assert_difference 'comment.reload.taggings_count', +1 do + tagging.taggable = comment + end + end + end end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 3dea7e1492..7f366b2c91 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -143,6 +143,7 @@ ActiveRecord::Schema.define do t.text :body, :null => false end t.string :type + t.integer :taggings_count, :default => 0 end create_table :companies, :force => true do |t| |