From c67ce5d0f463fc3f697401dafd6014564e5ead73 Mon Sep 17 00:00:00 2001 From: Adam Cooper Date: Wed, 31 Dec 2008 01:43:13 -0800 Subject: [PATCH] fixed an issue with reassigning a belongs_to association that has a namespaced model with a counter cache --- activerecord/lib/active_record/associations.rb | 4 +--- activerecord/lib/active_record/reflection.rb | 2 +- .../associations/belongs_to_associations_test.rb | 17 +++++++++++++++++ activerecord/test/models/reply.rb | 6 ++++++ activerecord/test/models/topic.rb | 6 ++++++ 5 files changed, 31 insertions(+), 4 deletions(-) diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 86616ab..dbc70e6 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1036,9 +1036,7 @@ module ActiveRecord # Create the callbacks to update counter cache if options[:counter_cache] - cache_column = options[:counter_cache] == true ? - "#{self.to_s.demodulize.underscore.pluralize}_count" : - options[:counter_cache] + cache_column = reflection.counter_cache_column method_name = "belongs_to_counter_cache_after_create_for_#{reflection.name}".to_sym define_method(method_name) do diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index dbff4f2..ab43826 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -192,7 +192,7 @@ module ActiveRecord def counter_cache_column if options[:counter_cache] == true - "#{active_record.name.underscore.pluralize}_count" + "#{active_record.name.demodulize.underscore.pluralize}_count" elsif options[:counter_cache] options[:counter_cache] end diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb index 40a8503..9b32663 100644 --- a/activerecord/test/cases/associations/belongs_to_associations_test.rb +++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb @@ -153,6 +153,23 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase assert_equal 0, Topic.find(t1.id).replies.size assert_equal 0, Topic.find(t2.id).replies.size end + + def test_belongs_to_reassign_with_namespaced_models_and_counters + t1 = Web::Topic.create("title" => "t1") + t2 = Web::Topic.create("title" => "t2") + r1 = Web::Reply.new("title" => "r1", "content" => "r1") + r1.topic = t1 + + assert r1.save + assert_equal 1, Web::Topic.find(t1.id).replies.size + assert_equal 0, Web::Topic.find(t2.id).replies.size + + r1.topic = Web::Topic.find(t2.id) + + assert r1.save + assert_equal 0, Web::Topic.find(t1.id).replies.size + assert_equal 1, Web::Topic.find(t2.id).replies.size + end def test_belongs_to_counter_after_save topic = Topic.create!(:title => "monday night") diff --git a/activerecord/test/models/reply.rb b/activerecord/test/models/reply.rb index 812bc1f..1c990ac 100644 --- a/activerecord/test/models/reply.rb +++ b/activerecord/test/models/reply.rb @@ -37,3 +37,9 @@ end class SillyReply < Reply belongs_to :reply, :foreign_key => "parent_id", :counter_cache => :replies_count end + +module Web + class Reply < Web::Topic + belongs_to :topic, :foreign_key => "parent_id", :counter_cache => true, :class_name => 'Web::Topic' + end +end \ No newline at end of file diff --git a/activerecord/test/models/topic.rb b/activerecord/test/models/topic.rb index 39ca1bf..44f8a14 100644 --- a/activerecord/test/models/topic.rb +++ b/activerecord/test/models/topic.rb @@ -67,3 +67,9 @@ class Topic < ActiveRecord::Base end end end + +module Web + class Topic < ActiveRecord::Base + has_many :replies, :dependent => :destroy, :foreign_key => "parent_id", :class_name => 'Web::Reply' + end +end \ No newline at end of file -- 1.5.6.2