diff options
author | Santiago Pastorino <santiago@wyeworks.com> | 2010-11-16 12:36:47 -0200 |
---|---|---|
committer | Santiago Pastorino <santiago@wyeworks.com> | 2010-11-16 12:40:40 -0200 |
commit | 4718d097ffe3af965f3ea7218156050507eabe4f (patch) | |
tree | 3e4435fb38bc4edd1d349c0aad4ed8cfa64aff05 | |
parent | dff0dfb7f80199e8f13dd20b39e0bc6b79438863 (diff) | |
download | rails-4718d097ffe3af965f3ea7218156050507eabe4f.tar.gz rails-4718d097ffe3af965f3ea7218156050507eabe4f.tar.bz2 rails-4718d097ffe3af965f3ea7218156050507eabe4f.zip |
Models should be equals even after destroyed
[#5978 state:committed]
-rw-r--r-- | activerecord/lib/active_record/base.rb | 6 | ||||
-rw-r--r-- | activerecord/test/cases/base_test.rb | 9 | ||||
-rw-r--r-- | activerecord/test/cases/named_scope_test.rb | 2 |
3 files changed, 13 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index f588475bbf..658c6d7b45 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1585,9 +1585,9 @@ MSG # 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) || - persisted? && - (comparison_object.instance_of?(self.class) && - comparison_object.id == id) + comparison_object.instance_of?(self.class) && + id.present? && + comparison_object.id == id end # Delegates to == diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 9f2b0c9c86..73c76606ad 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -397,6 +397,15 @@ class BasicsTest < ActiveRecord::TestCase assert_not_equal Topic.new, Topic.new end + def test_equality_of_destroyed_records + topic_1 = Topic.new(:title => 'test_1') + topic_1.save + topic_2 = Topic.find(topic_1.id) + topic_1.destroy + assert_equal topic_1, topic_2 + assert_equal topic_2, topic_1 + end + def test_hashing assert_equal [ Topic.find(1) ], [ Topic.find(2).topic ] & [ Topic.find(1) ] end diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb index fb24c65fff..6ac3e3fc56 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -122,7 +122,7 @@ class NamedScopeTest < ActiveRecord::TestCase :joins => 'JOIN authors ON authors.id = posts.author_id', :conditions => [ 'authors.author_address_id = ?', address.id ] ) - assert_equal posts_with_authors_at_address_titles, Post.with_authors_at_address(address).find(:all, :select => 'title') + assert_equal posts_with_authors_at_address_titles.map(&:title), Post.with_authors_at_address(address).find(:all, :select => 'title').map(&:title) end def test_scope_with_object |