我已经扩展了ActiveRecord :: Base类,如下所示:
LIB / activerecord_ext.rb:
class ActiveRecord::Base
named_scope(
:recent,
:conditions => ['created_at > ?', (Time.new - 3.day)],
:order => 'created_at DESC',
:limit => 5
)
end
在config / environment.rb中:
require "activerecord_ext"
这样可以正常启用类缓存。当我设置
config.cache_classes = true
我收到此错误:
>> 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
我认为我对包含扩展名做错了。任何帮助将不胜感激。
答案 0 :(得分:1)
在require 'activerecord_ext'
行之前或之后config.cache_classes = true
?无论如何,请尝试将require'activerecord_ext'放在初始化程序中。