The current state of default scopes now is broken (for me). I have tried using them many times but they always end up being more trouble then they are worth.
One of the biggest shortcomings I’ve had is the inability to remove just the default scope from the relation as sreid mentioned.
Unscoped does what it says. It removes all scopes from the relation. It would be nice to have another method that only nullified default scope. Something like ‘custom_scope’. Not sure if that’s quite the same intent as th OP, but if included it would be cause enough for me to try default scopes again.
I don’t think it makes sense for unscoped to return a different result based on the context. In general, we need a way to unscope any relation and that’s what unscoped is for. That said, we can provide a way to achieve what you want, but that should not be unscoped job.
The reason the unscoped with a block works is related to the ordering, you can think that you are unscoping the orders before you get the relation from @user:
Order.unscoped do # We are unscoping here
@user.orders # Now we get the relation
end
As workaround, you can either use the block or try this (I haven’t tried this yet, it may work or not):
class Order < AR::Base
scope :unfiltered, scoped
default_scope order(…)
end
The scope call must come before default_scope.
Finally, I am familiar with the complaints about default_scope, the solution is simple though: simply don’t use it (not being rude, that’s simply what I do most of the time).