Skip to content

Rails warning: Overwriting existing method <model_name>.open #31234

Closed

Description

@DLawla

Steps to reproduce

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

Activity

  1. georgeclaghorn commented on Nov 26, 2017

    @georgeclaghorn
    Contributor

    Does your Issue model, by any chance, declare an enum with an open value before that scope?

  2. DLawla commented on Nov 26, 2017

    @DLawla
    Author

    Good thinking, but I can confirm that it does not include an open value.

  3. georgeclaghorn commented on Nov 26, 2017

    @georgeclaghorn
    Contributor

    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.

  4. matthewd commented on Nov 26, 2017

    @matthewd
    Member

    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.

  5. DLawla commented on Nov 27, 2017

    @DLawla
    Author

    @matthewd Good call on pointing out Kernel here and my mis-interpretation of the warning. Cheers 👍

  6. ebrett commented on Aug 6, 2018

    @ebrett

    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.

  7. bf4 commented on Dec 12, 2018

    @bf4
    Contributor

    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

      class << self; undef_method :open; end # the enum overrides the Kernel#open method which we don't care about
      enum status: { open: 0, closed: 1 }

    edit per @ansonhoyt #31234 (comment)

      singleton_class.undef_method :open # the enum overrides the Kernel#open method which we don't care about
      enum status: { open: 0, closed: 1 }
  8. ansonhoyt commented on Apr 21, 2021

    @ansonhoyt
    Contributor

    @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:

    class Reservation < ApplicationRecord
      enum status: [:pending, :requested, :confirmed, :checked_out, :returned, :cancelled]
    
      singleton_class.undef_method :open # hide Kernel.open, avoiding a warning when defining scope :open
      scope :open, -> { where(status: [:pending, :requested]) }
    end
  9. nroose commented on Nov 5, 2021

    @nroose

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions