From 9c58b3d6f6846896e2bd17c2e5d0fdb9b7d82006 Mon Sep 17 00:00:00 2001 From: Tanja Otto Date: Sun, 16 May 2010 21:03:14 +0200 Subject: [PATCH 1/2] adapted testcases for #2907 of Balint Erdi because apply of his diff doesn't work in master [#2907 state:commited] --- .../associations/belongs_to_associations_test.rb | 6 +++--- activerecord/test/cases/base_test.rb | 2 +- activerecord/test/cases/reflection_test.rb | 6 +++--- activerecord/test/models/reply.rb | 2 +- activerecord/test/schema/schema.rb | 1 + 5 files changed, 9 insertions(+), 8 deletions(-) diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb index be77ee4..dff55c5 100644 --- a/activerecord/test/cases/associations/belongs_to_associations_test.rb +++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb @@ -328,15 +328,15 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase def test_custom_counter_cache reply = Reply.create(:title => "re: zoom", :content => "speedy quick!") - assert_equal 0, reply[:replies_count] + assert_equal 0, reply[:inane_replies_count] silly = SillyReply.create(:title => "gaga", :content => "boo-boo") silly.reply = reply - assert_equal 1, reply.reload[:replies_count] + assert_equal 1, reply.reload[:inane_replies_count] assert_equal 1, reply.replies.size - reply[:replies_count] = 17 + reply[:inane_replies_count] = 17 assert_equal 17, reply.replies.size end diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index b7ae619..79621cf 100755 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -2237,7 +2237,7 @@ class BasicsTest < ActiveRecord::TestCase def test_inspect_instance topic = topics(:first) - assert_equal %(#), topic.inspect + assert_equal %(#), topic.inspect end def test_inspect_new_instance diff --git a/activerecord/test/cases/reflection_test.rb b/activerecord/test/cases/reflection_test.rb index 6781862..d99d985 100644 --- a/activerecord/test/cases/reflection_test.rb +++ b/activerecord/test/cases/reflection_test.rb @@ -24,18 +24,18 @@ class ReflectionTest < ActiveRecord::TestCase def test_read_attribute_names assert_equal( - %w( id title author_name author_email_address bonus_time written_on last_read content approved replies_count parent_id parent_title type ).sort, + %w( id title author_name author_email_address bonus_time written_on last_read content approved replies_count inane_replies_count parent_id parent_title type ).sort, @first.attribute_names ) end def test_columns - assert_equal 13, Topic.columns.length + assert_equal 14, Topic.columns.length end def test_columns_are_returned_in_the_order_they_were_declared column_names = Topic.columns.map { |column| column.name } - assert_equal %w(id title author_name author_email_address written_on bonus_time last_read content approved replies_count parent_id parent_title type), column_names + assert_equal %w(id title author_name author_email_address written_on bonus_time last_read content approved replies_count inane_replies_count parent_id parent_title type), column_names end def test_content_columns diff --git a/activerecord/test/models/reply.rb b/activerecord/test/models/reply.rb index 6cc9ee0..2e71bc4 100644 --- a/activerecord/test/models/reply.rb +++ b/activerecord/test/models/reply.rb @@ -47,7 +47,7 @@ class WrongReply < Reply end class SillyReply < Reply - belongs_to :reply, :foreign_key => "parent_id", :counter_cache => :replies_count + belongs_to :reply, :foreign_key => "parent_id", :counter_cache => :inane_replies_count end module Web diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index f5fba2f..fbd9c4e 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -482,6 +482,7 @@ ActiveRecord::Schema.define do end t.boolean :approved, :default => true t.integer :replies_count, :default => 0 + t.integer :inane_replies_count, :default => 0 t.integer :parent_id t.string :parent_title t.string :type -- 1.7.0.3 From 988a4951373d098f561dce8e20c0fb60f3d7b2c2 Mon Sep 17 00:00:00 2001 From: Tanja Otto Date: Sun, 16 May 2010 22:12:55 +0200 Subject: [PATCH 2/2] adapted fix for #2907 of Balint Erdi because apply of his diff doesn't work in master [#2907 state:commited] --- .../associations/has_many_association.rb | 7 ++++++- 1 files changed, 6 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index 0464e8c..af0ff0e 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -57,7 +57,12 @@ module ActiveRecord end def cached_counter_attribute_name - "#{@reflection.name}_count" + ( belongs_to_association_in_target && belongs_to_association_in_target.counter_cache_column ) || "#{@reflection.name}_count" + end + + def belongs_to_association_in_target + target_belongs_to_associations = @reflection.klass.reflect_on_all_associations.select(&:belongs_to?) + target_belongs_to_associations.detect { |a| a.class_name == @owner.class.to_s } end def insert_record(record, force = false, validate = true) -- 1.7.0.3