Skip to content

Commit 6be82fb

Browse files
MONGOID-5737 Fix issue where <=> errors if there's a non-document (#5910)
* bug fix and test update * typo
1 parent c2ae3fe commit 6be82fb

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

lib/mongoid/equality.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module Equality
1818
#
1919
# @return [ Integer ] -1, 0, 1.
2020
def <=>(other)
21+
return super unless other.is_a?(Mongoid::Equality)
2122
attributes["_id"].to_s <=> other.attributes["_id"].to_s
2223
end
2324

spec/mongoid/equality_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,12 @@
189189
it "compares based on the document id" do
190190
expect(first <=> second).to eq(-1)
191191
end
192+
193+
it "doesn't break if one isn't a document" do
194+
expect do
195+
first <=> "Foo"
196+
end.to_not raise_error
197+
end
192198
end
193199

194200
describe "#eql?" do

0 commit comments

Comments
 (0)