Permalink
Browse files

Support for :counter_cache on polymorphic belongs_to

  • Loading branch information...
1 parent bea4065 commit 3c400627eb9cfac380d716ccf1182d61db4a45a6 @jonleighton jonleighton committed Dec 31, 2010
@@ -46,7 +46,7 @@ def update_counters(record)
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
@@ -567,4 +567,16 @@ def test_reloading_association_with_key_change
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
@@ -143,6 +143,7 @@ def create_table(*args, &block)
t.text :body, :null => false
end
t.string :type
+ t.integer :taggings_count, :default => 0
end
create_table :companies, :force => true do |t|

0 comments on commit 3c40062

Please sign in to comment.