2015-09-20 108 views
0

我想創建測試數據庫。開發數據庫具有以下設置:rake db:test:load,未定義方法`[]'爲零:NilClass

development: 
    adapter: mysql2 
    encoding: utf8 
    database: c9 
    username: <%=ENV['C9_USER']%> 
    host: <%=ENV['IP']%> 

當我運行命令:耙分貝:測試:負載--trace,顯示的錯誤:

rake aborted! 
NoMethodError: undefined method `[]' for nil:NilClass 
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-3.2.22/lib/active_record/railties/databases.rake:526:in `block (3 levels) in <top (required)>' 
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/rake-10.4.2/lib/rake/task.rb:240:in `call' 
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/rake-10.4.2/lib/rake/task.rb:240:in `block in execute' 
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/rake-10.4.2/lib/rake/task.rb:235:in `each' 
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/rake-10.4.2/lib/rake/task.rb:235:in `execute' 
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/rake-10.4.2/lib/rake/task.rb:179:in `block in invoke_with_call_chain' 
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize' 
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/rake-10.4.2/lib/rake/task.rb:172:in `invoke_with_call_chain' 
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/rake-10.4.2/lib/rake/task.rb:201:in `block in invoke_prerequisites' 
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/rake-10.4.2/lib/rake/task.rb:199:in `each' 
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/rake-10.4.2/lib/rake/task.rb:199:in `invoke_prerequisites' 
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/rake-10.4.2/lib/rake/task.rb:178:in `block in invoke_with_call_chain' 
/usr/local/rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize' 
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/rake-10.4.2/lib/rake/task.rb:172:in `invoke_with_call_chain' 
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/rake-10.4.2/lib/rake/task.rb:165:in `invoke' 
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/rake-10.4.2/lib/rake/application.rb:150:in `invoke_task' 
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/rake-10.4.2/lib/rake/application.rb:106:in `block (2 levels) in top_level' 
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/rake-10.4.2/lib/rake/application.rb:106:in `each' 
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/rake-10.4.2/lib/rake/application.rb:106:in `block in top_level' 
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/rake-10.4.2/lib/rake/application.rb:115:in `run_with_threads' 
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/rake-10.4.2/lib/rake/application.rb:100:in `top_level' 
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/rake-10.4.2/lib/rake/application.rb:78:in `block in run' 
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/rake-10.4.2/lib/rake/application.rb:176:in `standard_exception_handling' 
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/rake-10.4.2/lib/rake/application.rb:75:in `run' 
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/rake-10.4.2/bin/rake:33:in `<top (required)>' 
/usr/local/rvm/gems/ruby-2.0.0-p247/bin/rake:23:in `load' 
/usr/local/rvm/gems/ruby-2.0.0-p247/bin/rake:23:in `<main>' 
/usr/local/rvm/gems/ruby-2.0.0-p247/bin/ruby_executable_hooks:15:in `eval' 
/usr/local/rvm/gems/ruby-2.0.0-p247/bin/ruby_executable_hooks:15:in `<main>' 
Tasks: TOP => db:test:load => db:test:purge 

如何解決這個問題?

+0

爲什麼要用'development',爲什麼不創建一個'在你的數據庫配置test'配置? – Mutahhir

回答

2

添加測試ENV配置在您的database.yml。

還要記住爲你的測試數據庫使用不同的database_name。

test: 
    adapter: mysql2 
    encoding: utf8 
    database: c9_test 
    username: <%=ENV['C9_USER']%> 
    host: <%=ENV['IP']%> 

然後打這個命令

RAILS_ENV=test rake db:setup

+0

謝謝,很好 –

1

您可能希望運行具有傳遞給它的特定環境的命令。 可以這麼說:

bundle exec rake db:test:load RAILS_ENV="development"

+0

我嘗試過,但同樣的錯誤:耙子中止! NoMethodError:未定義方法'[]'爲零:NilClass /usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-3.2.22/lib/active_record/railties/databases.rake:526 :在中的塊(3級)中' /usr/local/rvm/gems/ruby-2.0.0-p247/bin/ruby_executable_hooks:15:in'eval' /usr/local/rvm/gems/ruby​​-2.0.0-p247/bin/ruby​​_executable_hooks:15:在'

' 任務:TOP => db:test:load => db:test:purge –

+0

謝謝,這個絕對解決了我的問題 – dps

相關問題