Skip to content

Commit

Permalink
Fix exception if old and new targets are both nil. Fixes rails#1471.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonleighton committed Jul 11, 2011
1 parent 5c8be9e commit 1712d90
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def update_counters(record)
# 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -681,4 +681,13 @@ def test_should_set_foreign_key_on_create_association!
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
5 changes: 4 additions & 1 deletion activerecord/test/models/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions activerecord/test/schema/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ def create_table(*args, &block)
end
t.string :type
t.integer :taggings_count, :default => 0
t.integer :children_count, :default => 0
end

create_table :companies, :force => true do |t|
Expand Down

0 comments on commit 1712d90

Please sign in to comment.