I'm using the gem discarded which adds a scope with_discarded on every model on which it is loaded on. To write some code that works with any model I added with_discarded scope (via a class method) on all models in application_record that does nothing so that then it will get overridden in those models which include the module from the discarded gem.
However, rails now produces a warning: Creating scope:with_discarded. Overwriting existing method User.with_discarded
Creating scope:with_discarded. Overwriting existing method User.with_discarded
every time I load a class that overwrites the base method. For completeness that's implemented just as:
def self.with_discarded
all
end
Now everything seems to be working fine, but I'm wondering what the correct way to do this would be and why I'm being warned. Am I supposed to define it as a scope rather than a class method (EDIT: tried it still get the warning)? Alternatively, is there a way to just filter this warning out of all my logs without loading a gem like Semantic Logger. Problem is that this warning shows up every time my GoodJob Scheduler runs and is polluting my logs.
So I followed @tadman's suggestion and just implemented a new method as follows
def self.include_discarded
respond_to?(:with_discarded) ? with_discarded : all
end
This doesn't quite let me overload the original with_discarded scope in the sense that I had to switch all the uses to include_discarded but it has the same effect. You could try and give the method the same name via metaprogramming but then you'll need a good way to run code in every subclass of ApplicationRecord.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.