aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorJamis Buck <jamis@37signals.com>2006-03-04 19:42:41 +0000
committerJamis Buck <jamis@37signals.com>2006-03-04 19:42:41 +0000
commitff881c7f8b1ffd855c09cabcd2c9b06aea4e850d (patch)
treed8c5aeeda94ad90d5a8a9a1ea4950bc9a5d585ae /activerecord/test
parenteb01d35109897162a48ff5219dcd97f35328168c (diff)
downloadrails-ff881c7f8b1ffd855c09cabcd2c9b06aea4e850d.tar.gz
rails-ff881c7f8b1ffd855c09cabcd2c9b06aea4e850d.tar.bz2
rails-ff881c7f8b1ffd855c09cabcd2c9b06aea4e850d.zip
Make counter cache work when replacing an association (closes #3245). Thanks for the patch!
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3762 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test')
-rwxr-xr-xactiverecord/test/associations_test.rb32
1 files changed, 32 insertions, 0 deletions
diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb
index c6cf7807fa..f687d3781e 100755
--- a/activerecord/test/associations_test.rb
+++ b/activerecord/test/associations_test.rb
@@ -829,6 +829,38 @@ class BelongsToAssociationsTest < Test::Unit::TestCase
assert_equal 0, Topic.find(debate.id).send(:read_attribute, "replies_count"), "First reply deleted"
end
+ def test_belongs_to_counter_with_reassigning
+ t1 = Topic.create("title" => "t1")
+ t2 = Topic.create("title" => "t2")
+ r1 = Reply.new("title" => "r1", "content" => "r1")
+ r1.topic = t1
+
+ assert r1.save
+ assert_equal 1, Topic.find(t1.id).replies.size
+ assert_equal 0, Topic.find(t2.id).replies.size
+
+ r1.topic = Topic.find(t2.id)
+
+ assert r1.save
+ assert_equal 0, Topic.find(t1.id).replies.size
+ assert_equal 1, Topic.find(t2.id).replies.size
+
+ r1.topic = nil
+
+ assert_equal 0, Topic.find(t1.id).replies.size
+ assert_equal 0, Topic.find(t2.id).replies.size
+
+ r1.topic = t1
+
+ assert_equal 1, Topic.find(t1.id).replies.size
+ assert_equal 0, Topic.find(t2.id).replies.size
+
+ r1.destroy
+
+ assert_equal 0, Topic.find(t1.id).replies.size
+ assert_equal 0, Topic.find(t2.id).replies.size
+ end
+
def test_assignment_before_parent_saved
client = Client.find(:first)
apple = Firm.new("name" => "Apple")