diff --git a/lib/couchbase-orm/base.rb b/lib/couchbase-orm/base.rb index 0f4d98cd..7cc6349b 100644 --- a/lib/couchbase-orm/base.rb +++ b/lib/couchbase-orm/base.rb @@ -246,11 +246,14 @@ def find(*ids, quiet: false) ids = ids.flatten.select { |id| id.present? } if ids.empty? - raise CouchbaseOrm::Error::EmptyNotAllowed, 'no id(s) provided' + raise CouchbaseOrm::Error::EmptyNotAllowed, 'no id(s) provided' unless quiet + return nil if quiet end transcoder = CouchbaseOrm::JsonTranscoder.new(ignored_properties: ignored_properties) records = quiet ? collection.get_multi(ids, transcoder: transcoder) : collection.get_multi!(ids, transcoder: transcoder) + return nil if records.nil? + CouchbaseOrm.logger.debug { "Base.find found(#{records})" } records = records.zip(ids).map { |record, id| self.new(record, id: id) if record diff --git a/spec/base_spec.rb b/spec/base_spec.rb index f2e33d2c..7fd18c7f 100644 --- a/spec/base_spec.rb +++ b/spec/base_spec.rb @@ -212,6 +212,14 @@ class BaseTestWithIgnoredProperties < CouchbaseOrm::Base expect(base.created_at).to be_a(Time) end + it 'should raise error when get object by nil id with quiet as false' do + expect { BaseTest.find(nil, quiet: false) }.to raise_error(CouchbaseOrm::Error::EmptyNotAllowed) + end + + it 'should not raise error when get object by nil id with quiet as true' do + expect { BaseTest.find(nil, quiet: true) }.not_to raise_error + end + describe BaseTest do it_behaves_like "ActiveModel" end