Rails / ActiveRecord – AdapterNotSpecified,即使它是

我正在做Ruby on Rails教程。 对于前三章,它使用SQLite,但后来它建议在开发中使用PostgreSQL以便更容易地进行Heroku部署。 在编辑我的database.ymlGemfile以使用pg而不是sqlite3之后,它似乎有效 – 除非使用Rake运行测试。 它会弹出AdapterNotSpecified错误。

C:/Ruby/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-4.0.0/lib/active_record/ connection_adapters/connection_specification.rb:52:in resolve_hash_connection': database configuration does not specify adapter (ActiveRecord::AdapterNotSpecif ied)

等等

database.yml指定了一个适配器,如下所示:

 development: adapter: postgresql host: localhost username: nekkoru password: derpderp database: development encoding: UTF8 

这是怎么回事? 我使用的是Windows 7 x64,Ruby 1.9.3,Rails 4.0.0,PostgreSQL 9.3.0.1。

您没有为测试环境定义数据库。 您的database.yml文件应如下所示:

 development: adapter: postgresql host: localhost username: nekkoru password: derpderp database: development encoding: UTF8 test: adapter: postgresql host: localhost username: nekkoru password: derpderp database: test # or whatever the name is encoding: UTF8 

缩进是我的罪魁祸首。

Rails似乎有一种从yml文件中读取数据库类型(开发或生产)和数据库设置(适配器,主机,用户名等)的严格方法。

但是当然ActiveRecord错误消息太过分了……它让我觉得我错过了一个gem