Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Make counter_cache work with polymorphic belongs_to

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3756 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 964b67dd0eb2b43cc8097492c52ce777c534cd11 1 parent c0d2477
@jamis jamis authored
View
2  activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Make counter_cache work with polymorphic belongs_to [Jamis Buck]
+
* Fixed that calling HasOneProxy#build_model repeatedly would cause saving to happen #4058 [anna@wota.jp]
* Added Sybase database adapter that relies on the Sybase Open Client bindings (see http://raa.ruby-lang.org/project/sybase-ctlib) #3765 [John Sheets]. It's almost completely Active Record compliant, but has the following caveats:
View
24 activerecord/lib/active_record/associations.rb
@@ -506,22 +506,22 @@ def belongs_to(association_id, options = {})
EOF
end
- if options[:counter_cache]
- module_eval(
- "after_create '#{reflection.class_name}.increment_counter(\"#{self.to_s.underscore.pluralize + "_count"}\", #{reflection.primary_key_name})" +
- " unless #{reflection.name}.nil?'"
- )
-
- module_eval(
- "before_destroy '#{reflection.class_name}.decrement_counter(\"#{self.to_s.underscore.pluralize + "_count"}\", #{reflection.primary_key_name})" +
- " unless #{reflection.name}.nil?'"
- )
- end
-
# deprecated api
deprecated_has_association_method(reflection.name)
deprecated_association_comparison_method(reflection.name, reflection.class_name)
end
+
+ if options[:counter_cache]
+ module_eval(
+ "after_create '#{reflection.name}.class.increment_counter(\"#{self.to_s.underscore.pluralize + "_count"}\", #{reflection.primary_key_name})" +
+ " unless #{reflection.name}.nil?'"
+ )
+
+ module_eval(
+ "before_destroy '#{reflection.name}.class.decrement_counter(\"#{self.to_s.underscore.pluralize + "_count"}\", #{reflection.primary_key_name})" +
+ " unless #{reflection.name}.nil?'"
+ )
+ end
end
# Associates two classes via an intermediate join table. Unless the join table is explicitly specified as
View
8 activerecord/test/associations_join_model_test.rb
@@ -104,4 +104,12 @@ def test_has_many_going_through_join_model_with_custom_foreign_key
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
View
3  activerecord/test/fixtures/db_definitions/schema.rb
@@ -8,6 +8,7 @@
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 @@
t.column "author_id", :integer
end
+ add_column :posts, :taggings_count, :integer, :default => 0
+
end
View
2  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
Please sign in to comment.
Something went wrong with that request. Please try again.