diff options
author | Xavier Noria <fxn@hashref.com> | 2014-03-29 04:56:13 -0700 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2014-03-29 04:56:13 -0700 |
commit | 66b42a67af60025ce5f393e13589dfcdf9409334 (patch) | |
tree | c4a2997116b5475ac8240c6155a3b50d4dacc9f2 | |
parent | 3ae33961e0e88c5e66502a94af29cd4b4417758f (diff) | |
parent | 945dd254ae4a79705c2c05a683fc37a8c98c8aeb (diff) | |
download | rails-66b42a67af60025ce5f393e13589dfcdf9409334.tar.gz rails-66b42a67af60025ce5f393e13589dfcdf9409334.tar.bz2 rails-66b42a67af60025ce5f393e13589dfcdf9409334.zip |
Merge pull request #14526 from chancancode/fix_ar_equality
Ensure we are returning either `true` or `false` for `#==`
-rw-r--r-- | activerecord/lib/active_record/core.rb | 2 | ||||
-rw-r--r-- | activerecord/lib/active_record/reflection.rb | 2 | ||||
-rw-r--r-- | 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 @@ module ActiveRecord 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 @@ module ActiveRecord 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 @@ class BasicsTest < ActiveRecord::TestCase 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 @@ class BasicsTest < ActiveRecord::TestCase 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 @@ class BasicsTest < ActiveRecord::TestCase 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") |