From 77e2a09de24eda9c2c7f27d3ffad3449d21cc84f Mon Sep 17 00:00:00 2001 From: DarshanaVenkatesh <70602567+DarshanaVenkatesh@users.noreply.github.com> Date: Fri, 13 Dec 2024 09:56:35 -0800 Subject: [PATCH] MONGOID-5737 Fix issue where <=> errors if there's a non-document (backport for 8.1) --- lib/mongoid/equality.rb | 1 + spec/mongoid/equality_spec.rb | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/lib/mongoid/equality.rb b/lib/mongoid/equality.rb index daeabf0166..ddd65161bd 100644 --- a/lib/mongoid/equality.rb +++ b/lib/mongoid/equality.rb @@ -17,6 +17,7 @@ module Equality # # @return [ Integer ] -1, 0, 1. def <=>(other) + return super unless other.is_a?(Mongoid::Equality) attributes["_id"].to_s <=> other.attributes["_id"].to_s end diff --git a/spec/mongoid/equality_spec.rb b/spec/mongoid/equality_spec.rb index 164e8e4922..cfb245a3ae 100644 --- a/spec/mongoid/equality_spec.rb +++ b/spec/mongoid/equality_spec.rb @@ -291,6 +291,12 @@ it "compares based on the document id" do expect(first <=> second).to eq(-1) end + + it "doesn't break if one isn't a document" do + expect do + first <=> "Foo" + end.to_not raise_error + end end describe "#eql?" do