Skip to content

Instantly share code, notes, and snippets.

@kml
Created December 13, 2016 22:55
# encoding: utf-8
# https://github.com/mongodb/mongoid/blob/4789bfffd3cbfa065eb7be7f34fbac122278a0ae/lib/mongoid/criteria.rb#L324
require "mongoid/criteria"
module Mongoid
class Criteria
OnlyWhenDefaultScopeError = Class.new(StandardError)
alias :mongoid_criteria_only :only
private :mongoid_criteria_only
def only(*args)
if scoped? && options[:fields].present? && args.flatten != options[:fields].keys
raise OnlyWhenDefaultScopeError, "Using #{klass}#only for criteria with default_scope already defining field for projection.\nUse #{klass}#unsoped method."
end
mongoid_criteria_only(*args)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment