From 945dd254ae4a79705c2c05a683fc37a8c98c8aeb Mon Sep 17 00:00:00 2001 From: Godfrey Chan Date: Sat, 29 Mar 2014 04:27:23 -0700 Subject: [PATCH] Ensure we are returning either `true` or `false` for `#==` 460eb83d cused `ActiveRecord::Base#==` to sometimes return `nil` in some cases, this ensures we always return a boolean value. Also fixed a similar problem in AR reflections. --- activerecord/lib/active_record/core.rb | 2 +- activerecord/lib/active_record/reflection.rb | 2 +- activerecord/test/cases/base_test.rb | 13 +++++++------ 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb index d9aaf8597f..4e53f66005 100644 --- a/activerecord/lib/active_record/core.rb +++ b/activerecord/lib/active_record/core.rb @@ -299,7 +299,7 @@ def encode_with(coder) def ==(comparison_object) super || comparison_object.instance_of?(self.class) && - id && + !id.nil? && comparison_object.id == id end alias :eql? :== diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index bce7766501..03b5bdc46c 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -151,7 +151,7 @@ def ==(other_aggregation) super || other_aggregation.kind_of?(self.class) && name == other_aggregation.name && - other_aggregation.options && + !other_aggregation.options.nil? && active_record == other_aggregation.active_record end diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 6acb342d0b..4969344763 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -533,6 +533,7 @@ def test_find_by_slug def test_equality_of_new_records assert_not_equal Topic.new, Topic.new + assert_equal false, Topic.new == Topic.new end def test_equality_of_destroyed_records @@ -544,6 +545,12 @@ def test_equality_of_destroyed_records assert_equal topic_2, topic_1 end + def test_equality_with_blank_ids + one = Subscriber.new(:id => '') + two = Subscriber.new(:id => '') + assert_equal one, two + end + def test_hashing assert_equal [ Topic.find(1) ], [ Topic.find(2).topic ] & [ Topic.find(1) ] end @@ -578,12 +585,6 @@ def test_destroy_without_prepared_statement assert_equal nil, Topic.find_by_id(topic.id) end - def test_blank_ids - one = Subscriber.new(:id => '') - two = Subscriber.new(:id => '') - assert_equal one, two - end - def test_comparison_with_different_objects topic = Topic.create category = Category.create(:name => "comparison") -- GitLab