【发布时间】:2016-09-24 23:19:29
【问题描述】:
有一个奇怪的问题...我的测试一直在针对我的开发数据库运行。我在这里尝试了binding.pry 中的test_helper.rb:
# this is the top of the file
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
binding.pry
并注意到 3 件事:
-
ENV['RAILS_ENV']/Rails.env已正确设置为"test"。 -
Rails 配置似乎加载了正确的值:
Rails.application.config.database_configuration[Rails.env] # => {"adapter"=>"postgis", # "encoding"=>"unicode", # "pool"=>5, # "username"=>"user", # "host"=>"localhost", # "database"=>"db_test"} <= CORRECT -
...但 ActiveRecord 加载的值不正确:
ActiveRecord::Base.configurations[Rails.env] # => {"adapter"=>"postgis", # "encoding"=>"unicode", # "pool"=>5, # "username"=>"user", # "host"=>"localhost", # "port"=>5432, # "database"=>"db_development"} <= INCORRECT
此外,ActiveRecord::Base.configurations[Rails.env] 返回一个 port 键,而 Rails.application.config.database_configuration[Rails.env] 不返回。
为什么这些不同?这是我的配置:
default: &default
adapter: postgis
encoding: unicode
pool: 5
username: user
host: localhost
development:
<<: *default
database: db_development
test:
<<: *default
database: db_test
我greped 我的整个项目文件夹和config/database.yml 是我命名我的数据库的唯一地方,所以据我所知,没有另一个配置覆盖这个。
我正在运行rails 4.2.5。
救命!
【问题讨论】:
-
您的环境中是否设置了
DATABASE_URL? -
@AhmadSherif 呃,是的。删除它可以解决我的问题。想把它作为答案发布,我会奖励你赏金吗?谢谢。
标签: activerecord ruby-on-rails-4.2