提交 3c400627 编写于 作者: J Jon Leighton

Support for :counter_cache on polymorphic belongs_to

上级 bea4065d
......@@ -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|
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册