Skip to content

Commit

Permalink
Fixed comparison of Active Record objects so two new objects are not …
Browse files Browse the repository at this point in the history
…equal #2099 [deberg]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2172 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
dhh committed Sep 9, 2005
1 parent 74896c0 commit 0faca07
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
2 changes: 2 additions & 0 deletions activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*

* Fixed comparison of Active Record objects so two new objects are not equal #2099 [deberg]

* Fixed that the SQL Server adapter would sometimes return DBI::Timestamp objects instead of Time #2127 [Tom Ward]

* Added the instance methods #root and #ancestors on acts_as_tree and fixed siblings to not include the current node #2142, #2140 [coffee2code]
Expand Down
5 changes: 4 additions & 1 deletion activerecord/lib/active_record/base.rb
Expand Up @@ -1169,7 +1169,10 @@ def column_for_attribute(name)

# Returns true if the +comparison_object+ is the same object, or is of the same type and has the same id.
def ==(comparison_object)
comparison_object.equal?(self) or (comparison_object.instance_of?(self.class) and comparison_object.id == id)
comparison_object.equal?(self) ||
(comparison_object.instance_of?(self.class) &&
comparison_object.id == id &&
!comparison_object.new_record?)
end

# Delegates to ==
Expand Down
4 changes: 4 additions & 0 deletions activerecord/test/base_test.rb
Expand Up @@ -468,6 +468,10 @@ def test_equality
assert_equal Topic.find(1), Topic.find(2).parent
end

def test_equality_of_new_records
assert_not_equal Topic.new, Topic.new
end

def test_hashing
assert_equal [ Topic.find(1) ], [ Topic.find(2).parent ] & [ Topic.find(1) ]
end
Expand Down

0 comments on commit 0faca07

Please sign in to comment.