在AWS上使用postgresql的ActiveRecord :: Base.establish_connection

时间:2015-06-28 08:48:56

标签: ruby-on-rails ruby postgresql activerecord amazon-web-services

当我将database.yml配置为这样:

default: &default
  adapter: postgresql
  encoding: utf8
  pool: 5
  timeout: 5000

production:
  <<: *default
  host: my_db_address
  port: 5432
  database: my_db_name
  username: my_db_user_name
  password: my_db_password

< test and development ommited >

当我建立这样的连接时:

ActiveRecord::Base.establish_connection

它说ActiveRecord::AdapterNotSpecified - 'production'

它的工作原理如果我这样做:

ActiveRecord::Base.establish_connection(
    {:adapter => 'postgresql',
     :database => 'my_db_name',
     :host => 'my_db_address',
     :port => '5432',
     :username => 'my_db_user_name',
     :password => 'my_db_password'}
)

我宁愿从database.yml加载配置。我该怎么做?

我正在使用Rails 4.2.1和Postgres 9.4

2 个答案:

答案 0 :(得分:2)

以下是工作代码:

db_config = YAML.load_file('config/database.yml')
ActiveRecord::Base.establish_connection(db_config['production'])

答案 1 :(得分:-2)

只需将您的生产环境作为选项传递:

ActiveRecord::Base.establish_connection(:production)