2011-09-12 26 views
2

我正在將Rails 2.3.11應用程序升級到3.0.10。當我嘗試運行諸如rails console之類的任何rails腳本或運行我的單元測試時,我在development.rb文件中收到NameError在config/environments/development.rb中從lib中加載類的Rails 3問題

我打電話給一個我在lib中定義的類,但是當development.rb調用該類時,似乎該庫尚未加載。

我做這樣的事情:

config.cache_store = CustomMemcachedStore.new(Memcached.new(...)) 

我有一個文件lib/custom_memcached_store.rb聲明

class CustomMemcachedStore < ActiveSupport::Cache::Store 

我收到以下錯誤類:

~/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.8.7/lib/rake.rb:2503:in `const_missing': uninitialized constant CustomMemcachedStore (NameError) 
    from ~/app_name/config/environments/development.rb:20:in `block in <top (required)>' 

application.rb,我已經在使用

config.autoload_paths += Dir["#{Rails.root}/lib"] 

感謝您給我的任何幫助。

回答

1

您將需要明確地require該文件,而不是依靠自動加載。

這是因爲環境配置的加載在自動加載路徑設置之前的啓動過程中很早發生。

在某些情況下,你可以使用初始化工作,以插入配置代碼到工作的地方,通過這樣的:

initializer "my_setup", :before => "some_other_setup" do |app| 
    # ... 
end 

不幸的是,這是不是這些案件之一,作爲緩存設置up here,而自動加載路徑不會設置,直到here,緊接在boostrap_hook之前。

+0

謝謝,需求修復它。我非常感謝你解釋爲什麼使用bootstrap.rb的代碼,而不僅僅是告訴我使用require。 – keithepley

+0

很高興提供幫助。我不得不查找它來確定初始化程序的順序,所以我們都學到了一些東西:-) – numbers1311407