Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't have a scope called public anymore with Rails 4.1? #15032

Closed
joevandyk opened this issue May 8, 2014 · 3 comments
Closed

Can't have a scope called public anymore with Rails 4.1? #15032

joevandyk opened this issue May 8, 2014 · 3 comments

Comments

@joevandyk
Copy link
Contributor

On Rails 4.1, the following model

class Product < ActiveRecord::Base
  scope :public, where(true)
end

gives me this error:

ArgumentError: You tried to define a scope named "public" on the model "Product", but Active Record already defined a class method with the same name.
    from /usr/local/stow/ruby-2.1.1-gh-2014.3.18/lib/ruby/gems/2.1.0/gems/activerecord-4.1.1/lib/active_record/scoping/named.rb:143:in `scope'
    from /tmp/scope/app/models/product.rb:2:in `<class:Product>'
    from /tmp/scope/app/models/product.rb:1:in `<top (required)>'

There's no error on Rails 4.0.5. Is this an intended breaking change for 4.1?

I see that that Module.public exists http://ruby-doc.org/core-2.1.1/Module.html#method-i-public

irb(main):001:0> Product.method(:public)
=> #<Method: Class(Module)#public>
@gsamokovarov
Copy link
Contributor

Yes, it is intentional. See #14582 and the recent 7e8e91c for more details.

@joevandyk
Copy link
Contributor Author

@gsamokovarov Thanks!

@chancancode
Copy link
Member

Yes, this is a new thing on Rails 4.1. These scopes with "reserved" names has always been problematic and a cause for many subtle bugs, so we decided to fail loudly instead.

In this case, the conflict is with the Module.public method as you suggested. By defining a scope called public, it will add a class method called public to the model class, which overrides the one on Module the controls methods visibility.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants