Skip to content

Instantly share code, notes, and snippets.

@kml
Created December 13, 2016 22:55
Show Gist options
  • Save kml/79595771e183b3001be5a691ac1f319e to your computer and use it in GitHub Desktop.
Save kml/79595771e183b3001be5a691ac1f319e to your computer and use it in GitHub Desktop.
# 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