From 964b67dd0eb2b43cc8097492c52ce777c534cd11 Mon Sep 17 00:00:00 2001 From: Jamis Buck Date: Sat, 4 Mar 2006 15:11:17 +0000 Subject: Make counter_cache work with polymorphic belongs_to git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3756 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/test/associations_join_model_test.rb | 8 ++++++++ activerecord/test/fixtures/db_definitions/schema.rb | 3 +++ activerecord/test/fixtures/tagging.rb | 2 +- 3 files changed, 12 insertions(+), 1 deletion(-) (limited to 'activerecord/test') diff --git a/activerecord/test/associations_join_model_test.rb b/activerecord/test/associations_join_model_test.rb index f7ca3d6a84..a2f42f060e 100644 --- a/activerecord/test/associations_join_model_test.rb +++ b/activerecord/test/associations_join_model_test.rb @@ -104,4 +104,12 @@ class AssociationsJoinModelTest < Test::Unit::TestCase assert_equal [], posts(:thinking).authors assert_equal [authors(:mary)], posts(:authorless).authors end + + def test_belongs_to_polymorphic_with_counter_cache + assert_equal 0, posts(:welcome)[:taggings_count] + tagging = posts(:welcome).taggings.create(:tag => tags(:general)) + assert_equal 1, posts(:welcome, :reload)[:taggings_count] + tagging.destroy + assert posts(:welcome, :reload)[:taggings_count].zero? + end end diff --git a/activerecord/test/fixtures/db_definitions/schema.rb b/activerecord/test/fixtures/db_definitions/schema.rb index 1cf4dee7a5..f2e9eacb08 100644 --- a/activerecord/test/fixtures/db_definitions/schema.rb +++ b/activerecord/test/fixtures/db_definitions/schema.rb @@ -8,6 +8,7 @@ ActiveRecord::Schema.define do create_table "tags", :force => true do |t| t.column "name", :string + t.column :taggings_count, :integer, :default => 0 end create_table "categorizations", :force => true do |t| @@ -16,4 +17,6 @@ ActiveRecord::Schema.define do t.column "author_id", :integer end + add_column :posts, :taggings_count, :integer, :default => 0 + end \ No newline at end of file diff --git a/activerecord/test/fixtures/tagging.rb b/activerecord/test/fixtures/tagging.rb index 06d0144b5a..8bc844c370 100644 --- a/activerecord/test/fixtures/tagging.rb +++ b/activerecord/test/fixtures/tagging.rb @@ -1,4 +1,4 @@ class Tagging < ActiveRecord::Base belongs_to :tag - belongs_to :taggable, :polymorphic => true + belongs_to :taggable, :polymorphic => true, :counter_cache => true end \ No newline at end of file -- cgit v1.2.3