Rails 3 doesn’t allow “open” as a Named Scope
Rails 3 has involved rewriting pretty much the whole codebase,
Rails 3 gives off the fragrance of a coherent, cohesive framework.
But this one kind of annoys me.
/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)
So, in Rails 3, when defining a named scope, we can’t override an existing method.
This all makes sense, we don’t want to inadvertently break anything (although I’d hope your test would catch such a thing)
But unfortunately, every object in Ruby has an “open” method… so “open” suddenly is off the market for named scopes… bummer
I spent most of day trying to sort this out… writing a patch… trying to convince people to apply it… but the rails core guys wouldn’t budge.
So here’s a quick hack I put in an initializer
# config/initializers/allow_open_as_a_named_scope.rb
require 'active_record'
class ActiveRecord::Base
class << self
alias :_open :open
undef_method :open
end
end
And we’re done…
Kind of annoying though. I use “open” all the time, because it makes sense
I’d prefer it if active record just made a logger.warn, and let me make my own decisions
My Fork of rails, My Example of the bug, My Undef_Method hack