Skip to content
Browse files

comparing different classes returns nil

  • Loading branch information...
1 parent c5b43af commit f170045c591f6a13e04ff30532269f9dee620b5a @dmathieu dmathieu committed with tenderlove Jun 26, 2011
Showing with 11 additions and 1 deletion.
  1. +5 −1 activerecord/lib/active_record/base.rb
  2. +6 −0 activerecord/test/cases/base_test.rb
View
6 activerecord/lib/active_record/base.rb
@@ -1827,7 +1827,11 @@ def frozen?
# 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
View
6 activerecord/test/cases/base_test.rb
@@ -486,6 +486,12 @@ def test_comparison
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

0 comments on commit f170045

Please sign in to comment.
Something went wrong with that request. Please try again.