diff options
author | Jon Leighton <j@jonathanleighton.com> | 2011-07-12 00:04:01 +0100 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2011-07-12 00:14:45 +0100 |
commit | 1712d90050bd4b32a9ee93548b5eef05a69491f7 (patch) | |
tree | cf76c0d1847f96d8f71c30f10c741d46dbe6f4b1 | |
parent | 5c8be9eb864886d7ba9abba0d59786614cca2a5a (diff) | |
download | rails-1712d90050bd4b32a9ee93548b5eef05a69491f7.tar.gz rails-1712d90050bd4b32a9ee93548b5eef05a69491f7.tar.bz2 rails-1712d90050bd4b32a9ee93548b5eef05a69491f7.zip |
Fix exception if old and new targets are both nil. Fixes #1471.
4 files changed, 15 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/associations/belongs_to_association.rb b/activerecord/lib/active_record/associations/belongs_to_association.rb index c263edd2c6..adb6af7165 100644 --- a/activerecord/lib/active_record/associations/belongs_to_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_association.rb @@ -37,7 +37,7 @@ module ActiveRecord # Checks whether record is different to the current target, without loading it def different_target?(record) record.nil? && owner[reflection.foreign_key] || - record.id != owner[reflection.foreign_key] + record && record.id != owner[reflection.foreign_key] end def replace_keys(record) diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb index 4190f6d76d..818902beb5 100644 --- a/activerecord/test/cases/associations/belongs_to_associations_test.rb +++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb @@ -681,4 +681,13 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase firm = client.create_firm! :name => "baa" assert_equal firm.id, client.client_of end + + def test_self_referential_belongs_to_with_counter_cache_assigning_nil + comment = Comment.create! :post => posts(:thinking), :body => "fuu" + comment.parent = nil + comment.save! + + assert_equal nil, comment.reload.parent + assert_equal 0, comments(:greetings).reload.children_count + end end diff --git a/activerecord/test/models/comment.rb b/activerecord/test/models/comment.rb index 43650c0427..88b139d931 100644 --- a/activerecord/test/models/comment.rb +++ b/activerecord/test/models/comment.rb @@ -7,10 +7,13 @@ class Comment < ActiveRecord::Base :joins => :post, :conditions => { "posts.author_id" => 1 } scope :created - + belongs_to :post, :counter_cache => true has_many :ratings + has_many :children, :class_name => 'Comment', :foreign_key => :parent_id + belongs_to :parent, :class_name => 'Comment', :counter_cache => :children_count + def self.what_are_you 'a comment...' end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index e6cd274586..64e0452100 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -155,6 +155,7 @@ ActiveRecord::Schema.define do end t.string :type t.integer :taggings_count, :default => 0 + t.integer :children_count, :default => 0 end create_table :companies, :force => true do |t| |