Replies: 1 comment
-
Trying to work around this pain point. I am finding it nigh on impossible to unscope the subclass in way that allows me to change a default_scope defined in the parent because of this issue. Real-world example. The parent class defines a scope
which is resolved on the subclass first, then it's parent resulting in
Which you then can't unscope :( However, I have found that in storable.rb the if a duplicate key is found in a selector $and is always added. I don't see the value in adding duplicates. So the best way to avoid this issue is to never add an $and if the value is already set.
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Just found something this morning that is just really strange.
I have been working a project refactoring the default scope of some of our models. In some cases you need to unscope and reapply scopes. Just the nature of the beast. However I discovered that if you have a model that has a subclass the default_scope is applied twice.
So all is well in our Cat
But our Sphynx has duplicated selectors, there is no point to re-apply the default scope here if it is a duplicate. This might not seem problematic unless you're in my situation where I want to play with unscoping and rescoping. But...
This indicates that the duplication of scopes is a little peculiar, what we are seeing is basically the same as this
But it doesn't duplicate here
But it does replace it here.
which is where this becomes problematic. It's just not consistent. The default scopes in my mind should be smart enough to de-dupe. I appreciate that a default_scope in a subclass might stack with the parent class too.
In ActiveRecord successive where scopes append (with AND) to the original scope similar above. But ActiveRecord doesn't duplicate default_scope in the first place which I think where my personal issue is coming from. So it wouldn't then replace the $and clause contents with the most recent options, but even if it did why doesn't it replace the $and Array and not the 'lazy' key?
I can demonstrate in specs in this project
https://github.com/braindeaf/mongoid-where-oddity
Also opened a JIRA ticket
https://jira.mongodb.org/browse/MONGOID-5204
Many thanks
RobL
Beta Was this translation helpful? Give feedback.
All reactions