diff options
author | Michael Koziarski <michael@koziarski.com> | 2008-01-26 06:23:03 +0000 |
---|---|---|
committer | Michael Koziarski <michael@koziarski.com> | 2008-01-26 06:23:03 +0000 |
commit | c48f744400844e4c73eb91c83d86c3e915a9d78b (patch) | |
tree | 4ab9aab31349aebaeff17181d1170fdbcc486ebe | |
parent | 02625c9c962a868ee7d1a0903d5aaead290ec0ef (diff) | |
download | rails-c48f744400844e4c73eb91c83d86c3e915a9d78b.tar.gz rails-c48f744400844e4c73eb91c83d86c3e915a9d78b.tar.bz2 rails-c48f744400844e4c73eb91c83d86c3e915a9d78b.zip |
Make sure that belongs_to counter decrements when assigning nil Closes #10804 [jeanmartin]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8735 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r-- | activerecord/lib/active_record/associations/belongs_to_association.rb | 2 | ||||
-rwxr-xr-x | activerecord/test/cases/associations_test.rb | 12 | ||||
-rwxr-xr-x | activerecord/test/cases/base_test.rb | 2 | ||||
-rw-r--r-- | activerecord/test/fixtures/posts.yml | 1 | ||||
-rw-r--r-- | activerecord/test/models/comment.rb | 4 | ||||
-rw-r--r-- | activerecord/test/schema/schema.rb | 1 |
6 files changed, 18 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/associations/belongs_to_association.rb b/activerecord/lib/active_record/associations/belongs_to_association.rb index 1752678cbd..f5557618c4 100644 --- a/activerecord/lib/active_record/associations/belongs_to_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_association.rb @@ -13,7 +13,7 @@ module ActiveRecord counter_cache_name = @reflection.counter_cache_column if record.nil? - if counter_cache_name && @owner[counter_cache_name] && !@owner.new_record? + if counter_cache_name && !@owner.new_record? @reflection.klass.decrement_counter(counter_cache_name, @owner[@reflection.primary_key_name]) if @owner[@reflection.primary_key_name] end diff --git a/activerecord/test/cases/associations_test.rb b/activerecord/test/cases/associations_test.rb index a15abf90a6..0df508d01d 100755 --- a/activerecord/test/cases/associations_test.rb +++ b/activerecord/test/cases/associations_test.rb @@ -1308,6 +1308,18 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase assert_equal 0, Topic.find(debate.id).send(:read_attribute, "replies_count"), "First reply deleted" end + def test_belongs_to_counter_with_assigning_nil + p = Post.find(1) + c = Comment.find(1) + + assert_equal p.id, c.post_id + assert_equal 2, Post.find(p.id).comments.size + + c.post = nil + + assert_equal 1, Post.find(p.id).comments.size + end + def test_belongs_to_counter_with_reassigning t1 = Topic.create("title" => "t1") t2 = Topic.create("title" => "t2") diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 4339b725c8..04ed751eb5 100755 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -877,7 +877,7 @@ class BasicsTest < ActiveRecord::TestCase end def test_readonly_attributes - assert_equal Set.new([ 'title' ]), ReadonlyTitlePost.readonly_attributes + assert_equal Set.new([ 'title', 'comments_count' ]), ReadonlyTitlePost.readonly_attributes post = ReadonlyTitlePost.create(:title => "cannot change this", :body => "changeable") post.reload diff --git a/activerecord/test/fixtures/posts.yml b/activerecord/test/fixtures/posts.yml index 0f1445b638..92e5d1908f 100644 --- a/activerecord/test/fixtures/posts.yml +++ b/activerecord/test/fixtures/posts.yml @@ -3,6 +3,7 @@ welcome: author_id: 1 title: Welcome to the weblog body: Such a lovely day + comments_count: 2 type: Post thinking: diff --git a/activerecord/test/models/comment.rb b/activerecord/test/models/comment.rb index 60ca082c2e..73ebdadf73 100644 --- a/activerecord/test/models/comment.rb +++ b/activerecord/test/models/comment.rb @@ -1,5 +1,5 @@ class Comment < ActiveRecord::Base - belongs_to :post + belongs_to :post, :counter_cache => true def self.what_are_you 'a comment...' @@ -20,4 +20,4 @@ class VerySpecialComment < Comment def self.what_are_you 'a very special comment...' end -end
\ No newline at end of file +end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 307ad8b935..44bbb48773 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -154,6 +154,7 @@ ActiveRecord::Schema.define do t.string :title, :null => false t.text :body, :null => false t.string :type + t.integer :comments_count, :default => 0 end create_table :projects, :force => true do |t| |