class Foo < ActiveRecord::Base
default_scope { where(active: false) }
scope :include_deleted, -> { unscoped }
This does not work. It returns the scoped documents.
alias_method :include_deleted, :unscoped
This does not work either: undefined method 'unscoped'
So, how to define a scope that is unscoped?
Use Case: Yes, I know I can just use unscoped
. Let me attempt to explain why it may not be such a great idea. There is a good reason to define another scope that has the same function. The reason is that it makes the code easier to understand and it makes my intention more explicit. unscoped
does not tell the reader anything about the default scope. It only brings up more questions. If we can define another scope that explains why we are using the scope, then it provides context to the reader and helps them understand the code.
BTW, I saw some related SO questions, but they are old. So this question is about Rails 5. Which makes it different and not a duplicate.
Using Mongoid 5, but I don't think that will change the answer.
You can use ActiveRecord::QueryMethods#unscope
:
scope :include_deleted, -> { unscope(:where) }
P.S. Using default_scope
is usually a bad idea in the first place. But there are tons of articles about it, so I won't reason it here.