I've seen a lot of posts regarding this, but none seem to solve my problem. I have a default_scope
on a model like so:
default_scope where(:is_active => true).order('LOWER(table.name)');
I have other (normal) scopes, and I want to create an inactive
scope using unscoped
. I would like to define it as a scope, but it only works when defined as a class method:
# works
def self.inactive
unscoped { where(:is_active => false) }
end
# none of these work
scope :inactive, unscoped { where(:is_active => false) }
scope :inactive, with_exclusive_scope { where(:is_active => true) }
scope :inactive, unscoped.where(:is_active => false)
scope :inactive, lambda { unscoped { where(:is_active => false) } }
scope :inactive, unscoped { lambda { where(:is_active => false) } }
unscoped do
scope :inactive, where(:is_active => false)
end
Is there a way that I missed, or do I have to use a class method to define this scope?
except
andonly
but they don't seem to negate thedefault_scope
from within another scope, similar to the problem withunscoped
andwith_exclusive_scope
. – Vitreousscope :inactive, unscoped.where(:is_active => false)
works perfectly fine for me on a Rails 3.1.3 app. I tried it on aUser
class and callingUser.inactive
gives me only those records whereis_active
isfalse
. – Pentheasdefault_scope
set towhere(:is_active => true)
? – Scorescope
before or afterdefault_scope
? – Score