You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We have a model called Issue, which has an open scope: scope :open, -> { where.not(status: CLOSED_STATUSES) }
When we deploy our app, we are seeing the following console warning: W, [2017-11-24T03:58:31.015921 #4] WARN -- : Creating scope :open. Overwriting existing method Issue.open.
After deploying the app, we tested the Issue.open, and it performs as as programmed above -- so, the warning seems to be only just a warning and does not actually overwrite the method.
On first googling, we wondered if this old, closed issue was at all related: #8179.
What are your thoughts on this funny warning?
System configuration
Rails version:
5.2.0.alpha Ruby version:
2.4.2
Reacted by Aaron Gray, Jonas Innala, Andrew Crabb, Tom Raday, Aleksandr Moroz, Chris Drane, Lucas Caton, Tomas Skogberg, David Alejandro and jsh562
Something’s defining an open method on the Issue class and you’re subsequently overwriting it. I don’t see anything “funny“ here, and we can’t help further without looking at the app code.
Regardless, this isn’t the place to ask support questions. Please use the mailing list or Stack Overflow, where a larger community can help you.
Something’s defining an open method on the Issue class
The something is Ruby, and it's on Kernel.
I'm not sure how we can make the warning clearer; you seem to have interpreted it as a claim that something is overwriting your scope ("does not actually overwrite the method"), while that's the opposite of what it means to convey: your scope is itself overwriting the existing open method.
Reacted by Aaron Gray, Daniele Di Bernardo, Illia Yurtsiv, Carlos A. Carro Duplá, Pysis, Duccio Armenise, Maximilian Fischer, Jason Bosco, Tomas Skogberg, ravishwetha and 8 more
Reacted by Illia Yurtsiv, Duccio Armenise, Lucas Caton, Horacio Bertorello and Sergey Verevkin
I came looking for the same reason and interpreted the message the same way - it reads as if the Issue.open method is being overwritten rather than that it is overwriting the Kernel method. The implied subject of the second sentence is: [The new scope method :open is] overwriting Issue.open. This, to me, is the more natural reading of the message.
Reacted by Dan Lawler, Paul Mackay, denny, Maximilian Fischer, c2ofh, Chris Drane, Yoni Amir, Alex Spencer, ravishwetha, Andrew Foster and 1 more
since the output comes from the logger, we can either wrap the enum definition in logger.silence (which may not be available) or something like that, or what is probably more correct, what I've done, it to undef the offending method before
class << self;undef_method:open;end# the enum overrides the Kernel#open method which we don't care aboutenumstatus: {open: 0,closed: 1}
singleton_class.undef_method:open# the enum overrides the Kernel#open method which we don't care aboutenumstatus: {open: 0,closed: 1}
Reacted by Celso Pinto, Tom Raday, Benjamin Wood, Duccio Armenise, Sandro, Maximilian Fischer, Matiss Gaigals, Jacob Dalton, ishiyu, Yusuke Sangenya and 32 more
Reacted by DVThuanDL
Reacted by DVThuanDL
Reacted by DVThuanDL
Reacted by DVThuanDL
Reacted by Daniel Mircea, Lucas Caton, Alberto Gil, Yoni Amir, Paul Sandhu, Tomas Skogberg, Brady Bouchard, DVThuanDL and Anson Hoyt
classReservation < ApplicationRecordenumstatus: [:pending,:requested,:confirmed,:checked_out,:returned,:cancelled]singleton_class.undef_method:open# hide Kernel.open, avoiding a warning when defining scope :openscope:open,->{where(status: [:pending,:requested])}end
Reacted by Benjamin Fleischer, Ben Dilley, Andrew Burleson, Roger Yang, Lucas Azzola, Yuri Zinchenko, Steve Friedman, pcmd101, John Hume, Eduard Siebert and 1 more
Something’s defining an open method on the Issue class
The something is Ruby, and it's on Kernel.
I'm not sure how we can make the warning clearer; you seem to have interpreted it as a claim that something is overwriting your scope ("does not actually overwrite the method"), while that's the opposite of what it means to convey: your scope is itself overwriting the existing open method.
To me the problem is that the error message says the new method is being overwritten, and instead it should name both or it should say "by" or "with" before the new method. And it should have file and line number of both. It has the info, and the message is pretty much useless as is.
Activity
georgeclaghorn commentedon Nov 26, 2017
Does your
Issuemodel, by any chance, declare an enum with anopenvalue before that scope?DLawla commentedon Nov 26, 2017
Good thinking, but I can confirm that it does not include an
openvalue.georgeclaghorn commentedon Nov 26, 2017
Something’s defining an
openmethod on theIssueclass and you’re subsequently overwriting it. I don’t see anything “funny“ here, and we can’t help further without looking at the app code.Regardless, this isn’t the place to ask support questions. Please use the mailing list or Stack Overflow, where a larger community can help you.
matthewd commentedon Nov 26, 2017
The something is Ruby, and it's on Kernel.
I'm not sure how we can make the warning clearer; you seem to have interpreted it as a claim that something is overwriting your scope ("does not actually overwrite the method"), while that's the opposite of what it means to convey: your scope is itself overwriting the existing
openmethod.DLawla commentedon Nov 27, 2017
@matthewd Good call on pointing out Kernel here and my mis-interpretation of the warning. Cheers 👍
ebrett commentedon Aug 6, 2018
I came looking for the same reason and interpreted the message the same way - it reads as if the Issue.open method is being overwritten rather than that it is overwriting the Kernel method. The implied subject of the second sentence is: [The new scope method :open is] overwriting Issue.open. This, to me, is the more natural reading of the message.
bf4 commentedon Dec 12, 2018
For future googlers:
since the output comes from the logger, we can either wrap the enum definition in logger.silence (which may not be available) or something like that, or what is probably more correct, what I've done, it to undef the offending method before
edit per @ansonhoyt #31234 (comment)
ansonhoyt commentedon Apr 21, 2021
@bf4 great solution for an intentional override, thank you! I reworded it with Object.singleton_class (added in Ruby 1.9.2) so it reads better to my eyes:
nroose commentedon Nov 5, 2021
To me the problem is that the error message says the new method is being overwritten, and instead it should name both or it should say "by" or "with" before the new method. And it should have file and line number of both. It has the info, and the message is pretty much useless as is.