1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Support for :counter_cache on polymorphic belongs_to

This commit is contained in:
Jon Leighton 2010-12-31 18:36:02 +00:00
parent bea4065d3c
commit 3c400627eb
3 changed files with 14 additions and 1 deletions

View file

@ -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

View file

@ -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

View file

@ -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|