views:

212

answers:

1

Hi,

I have extended the ActiveRecord::Base class as follows:

lib/activerecord_ext.rb:

class ActiveRecord::Base
  named_scope( 
    :recent, 
    :conditions => ['created_at > ?', (Time.new - 3.day)], 
    :order => 'created_at DESC', 
    :limit => 5
  )
end

In config/environment.rb:

require "activerecord_ext"

This works fine until class caching is enabled. When I set

config.cache_classes = true

I get this error:

>> Person.recent
NoMethodError: You have a nil object when you didn't expect it!
The error occurred while evaluating nil.call
 from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.3/lib/active_record/named_scope.rb:102:in `recent'
 from (irb):1

I assume I'm doing something wrong with the inclusion of the extension. Any help would be greatly appreciated.

+1  A: 

Is require 'activerecord_ext' before or after the config.cache_classes = true line? In any case, try putting require 'activerecord_ext' in an initializer instead.

hgimenez
That did it, thanks.
Thilo