This project is archived and is in readonly mode.

#4083 ✓resolved
MatthewRudy

[Patch] Named Scopes should be allowed to overwrite an existing method

Reported by MatthewRudy | March 2nd, 2010 @ 03:38 AM

http://github.com/matthewrudy/rude-rails/commit/3a29b8fc473a15c321b...

I wrote a little blog post on this http://bit.ly/9LgFh0
but it's motivated by the following case

class MyClass < ActiveRecord::Base
  scope :open, :conditions => "start_date <= CURRENT_DATE"
end

I think this is a common pattern.
the word "open" makes sense in some cases,
more so than "active".

But in Rails 3 this doesn't work

/Users/matthew/.bundle/ruby/1.8/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/activerecord/lib/active_record/named_scope.rb:104:in `scope’: Cannot define scope :open because AbcDef.open method already exists. (ArgumentError)

This is because

>> MyClass.private_methods.include?("open")
=> true
>> "some string".private_methods.include?("open")
=> true

Everything has a private method called "open".

So what's the right solution.
I claim that I should be allowed to define a method of any particular name,
if I break something by overwriting an existing method,
then that's a mistake of mine
and I hope my testing will pick it up.

I claim a logger.warn is the correct functionality.

My branch to fix this: http://github.com/matthewrudy/rude-rails/tree/make_named_scopes_war...
My commit to fix this: http://github.com/matthewrudy/rude-rails/commit/3a29b8fc473a15c321b...

Also attached as a patch

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Attachments

Referenced by

Pages