diff options
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/base.rb | 6 | ||||
-rw-r--r-- | activerecord/test/cases/base_test.rb | 6 |
2 files changed, 11 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index a2b5a3ec4f..e970445082 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1806,7 +1806,11 @@ MSG # Allows sort on objects def <=>(other_object) - self.to_key <=> other_object.to_key + if other_object.is_a?(self.class) + self.to_key <=> other_object.to_key + else + nil + end end # Backport dup from 1.9 so that initialize_dup() gets called diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 873c6d9678..5fef3faab9 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -486,6 +486,12 @@ class BasicsTest < ActiveRecord::TestCase assert_equal [topic_2, topic_1].sort, [topic_1, topic_2] end + def test_comparison_with_different_objects + topic = Topic.create + category = Category.create(:name => "comparison") + assert_nil topic <=> category + end + def test_readonly_attributes assert_equal Set.new([ 'title' , 'comments_count' ]), ReadonlyTitlePost.readonly_attributes |