Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

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

#4083

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

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

State: resolved
Milestone: none
Assigned to: Pratik Pratik
Importance: none

Activity

  1. Repository
    Repository
    • State changed from new to resolved

    (from [b0967cc5cf5ce08e7fb0574692e3bb785e79973a]) defining a named_scope which overwrites an existing method is now allowed we just log a warning.

    This was motivated by the fact that :open is defined on all classes
    as such the named_scope "open" can never be used, without hacking
    ActiveRecord with an "undef_method" [#4083 [Patch] Named Scopes should be allowed to overwrite an existing method state:resolved]

    Signed-off-by: wycats wycats@gmail.com
    http://github.com/rails/rails/commit/b0967cc5cf5ce08e7fb0574692e3bb...

    March 28th, 2010 @ 09:53 PM