This project is archived and is in readonly mode.
[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
-
Repository March 28th, 2010 @ 09:53 PM
- 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 state:resolved]Signed-off-by: wycats wycats@gmail.com
http://github.com/rails/rails/commit/b0967cc5cf5ce08e7fb0574692e3bb...
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
Tags
Referenced by
- 4083 [Patch] Named Scopes should be allowed to overwrite an existing method This was motivated by the fact that :open is defined on a...