This project is archived and is in readonly mode.
Adding a scope to ActiveRecord:Base throws undefined method exception
Reported by Saimon Moore | April 22nd, 2010 @ 09:57 AM
#works
class User < ActiveRecord::Base
scope :random, lambda { |lt| where("created_at < ?", Time.now.utc).order('RAND()').limit(lt) }
end
#throws exception below
module ActiveRecord
class Base
scope :random, lambda { |lt| where("created_at < ?", Time.now.utc).order('RAND()').limit(lt) }
end
end
ruby-1.9.1-p378 > User.random(1).first
NoMethodError: undefined method abstract_class?' for
Object:Class<br/>
from /Users/saimon/.rvm/gems/ruby-1.9.1-p378@myapp/gems/activerecord-3.0.0.beta3/lib/active_record/base.rb:1335:in `class_of_active_record_descendant'
from /Users/saimon/.rvm/gems/ruby-1.9.1-p378@myapp/gems/activerecord-3.0.0.beta3/lib/active_record/base.rb:1000:in `base_class'
from /Users/saimon/.rvm/gems/ruby-1.9.1-p378@myapp/gems/activerecord-3.0.0.beta3/lib/active_record/base.rb:759:in `reset_table_name'
from /Users/saimon/.rvm/gems/ruby-1.9.1-p378@myapp/gems/activerecord-3.0.0.beta3/lib/active_record/base.rb:751:in `table_name'
from /Users/saimon/.rvm/gems/ruby-1.9.1-p378@myapp/gems/activerecord-3.0.0.beta3/lib/active_record/base.rb:1032:in `arel_table'
from /Users/saimon/.rvm/gems/ruby-1.9.1-p378@myapp/gems/activerecord-3.0.0.beta3/lib/active_record/base.rb:1027:in `unscoped'
from /Users/saimon/.rvm/gems/ruby-1.9.1-p378@myapp/gems/activerecord-3.0.0.beta3/lib/active_record/named_scope.rb:30:in `scoped'
from /Users/saimon/.rvm/gems/ruby-1.9.1-p378@myapp/gems/activerecord-3.0.0.beta3/lib/active_record/base.rb:407:in `rescue in where'
from /Users/saimon/.rvm/gems/ruby-1.9.1-p378@myapp/gems/activerecord-3.0.0.beta3/lib/active_record/base.rb:405:in `where'
from /Users/saimon/Development/Projects/myapp_r3/config/initializers/active_record.rb:4:in `block in <class:Base>'
from /Users/saimon/.rvm/gems/ruby-1.9.1-p378@myapp/gems/activerecord-3.0.0.beta3/lib/active_record/named_scope.rb:95:in `call'
from /Users/saimon/.rvm/gems/ruby-1.9.1-p378@myapp/gems/activerecord-3.0.0.beta3/lib/active_record/named_scope.rb:95:in `block in scope'
from /Users/saimon/.rvm/gems/ruby-1.9.1-p378@myapp/gems/activerecord-3.0.0.beta3/lib/active_record/named_scope.rb:104:in `call'
from /Users/saimon/.rvm/gems/ruby-1.9.1-p378@myapp/gems/activerecord-3.0.0.beta3/lib/active_record/named_scope.rb:104:in `block (2 levels) in scope'
from (irb):1
from /Users/saimon/.rvm/gems/ruby-1.9.1-p378@myapp/gems/railties-3.0.0.beta3/lib/rails/commands/console.rb:47:in `start'
from /Users/saimon/.rvm/gems/ruby-1.9.1-p378@myapp/gems/railties-3.0.0.beta3/lib/rails/commands/console.rb:8:in `start'
from /Users/saimon/.rvm/gems/ruby-1.9.1-p378@myapp/gems/railties-3.0.0.beta3/lib/rails/commands.rb:34:in `<top (required)>'
from script/rails:9:in `require'
from script/rails:9:in `<main>'ruby-1.9.1-p378 >
Comments and changes to this ticket
-
Saimon Moore April 22nd, 2010 @ 09:57 AM
- Tag set to activerecord rails3
-
Saimon Moore April 22nd, 2010 @ 09:58 AM
- Tag changed from activerecord rails3 to activerecord rails3, ruby-1.9.1
-
Piotr Sarnacki December 17th, 2010 @ 10:57 AM
- Importance changed from to Low
I don't know if we should support that kind of scopes declaration. The more appropriate in that case would be to just use class method, as you're reopening base class:
def self.random(lt) where("created_at < ?", Time.now.utc).order('RAND()').limit(lt) end
-
Duke February 19th, 2011 @ 03:44 AM
I'm running across this as well. Every model here has a possible polymorphic relationship with Starlight.
module ActiveRecord
class Basehas_many :starlights, :as => :entity, :dependent => :destroy def starlight Starlight.for(self) end scope :order_by_starlight, joins(:starlights).group('starlights.id').having('max(starlights.id)').order('starlights.value')
end end
I require order_by_starlight to be composable, so I can say, for instance, Entry.where(:user => current_user).order_by_starlightThis seems a good use case for adding scope to Activerecord::Base.
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>