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.

Adding a scope to ActiveRecord:Base throws undefined method exception

#4454
#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 >

Reported by Saimon Moore · April 22nd, 2010 @ 09:57 AM

State: new
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Saimon Moore
    Saimon Moore
    • Tag set to activerecord rails3

    April 22nd, 2010 @ 09:57 AM

  2. Saimon Moore
    Saimon Moore
    • Tag changed from activerecord rails3 to activerecord rails3, ruby-1.9.1

    April 22nd, 2010 @ 09:58 AM

  3. Piotr Sarnacki
    Piotr Sarnacki
    • 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
    

    December 17th, 2010 @ 10:57 AM

  4. Duke
    Duke

    I'm running across this as well. Every model here has a possible polymorphic relationship with Starlight.

    module ActiveRecord
    class Base

    has_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_starlight

    This seems a good use case for adding scope to Activerecord::Base.

    February 19th, 2011 @ 03:44 AM

  5. Duke
    Duke

    Yes, I see with Arel a class method is composable.

    March 16th, 2011 @ 08:11 AM

  6. bingbing