简体   繁体   English

在AWS上与Postgresql的ActiveRecord :: Base。Establishment_connection

[英]ActiveRecord::Base.establish_connection with postgresql on AWS

When I got my database.yml configured like this: 当我像这样配置我的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 >

When I establish connection like this: 当我建立这样的连接时:

ActiveRecord::Base.establish_connection

it says ActiveRecord::AdapterNotSpecified - 'production' 它说ActiveRecord::AdapterNotSpecified - 'production'

It works hovewer If I do it like this: 它的工作原理如果我这样做是这样的:

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'}
)

I'd rather load the config from database.yml. 我宁愿从database.yml加载配置。 How do I do this? 我该怎么做呢?

I'm on Rails 4.2.1 and Postgres 9.4 我在Rails 4.2.1和Postgres 9.4上

Here is the working code: 这是工作代码:

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

只需通过您的生产环境作为选择:

ActiveRecord::Base.establish_connection(:production)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Rails中ActiveRecord :: Base。Establishment_connection有什么影响? - What is the impact of ActiveRecord::Base.establish_connection in Rails? 红宝石死于“ ActiveRecord :: Base。Establishment_connection” - ruby just dies on “ActiveRecord::Base.establish_connection” ActiveRecord :: Base。Establishment_connection连接到错误的数据库 - ActiveRecord::Base.establish_connection connects to wrong database 不同模型中的ActiveRecord :: Base。Establishment_connection是否使用相同的连接池? - Does ActiveRecord::Base.establish_connection in different models use same connection pool? 动态子类化和ActiveRecord建立与PostgreSQL数据库的连接 - Dynamic Subclassing and ActiveRecord establish_connection with PostgreSQL database Mongoid中的ActiveRecord#Establishment_connection等效项是什么? - what is the equivalent of ActiveRecord#establish_connection in Mongoid? 拦截ActiveRecord建立连接初始化过程 - Intercept ActiveRecord establish_connection initialization process 了解establish_connection如何在ActiveRecord中工作 - Understanding how establish_connection works in ActiveRecord ActiveRecord::ConnectionNotEstablished: ActiveRecord::Base with mongoid 没有连接池 - ActiveRecord::ConnectionNotEstablished: No connection pool for ActiveRecord::Base with mongoid Rails:没有 ActiveRecord::Base 的连接池 - Rails: No connection pool for ActiveRecord::Base
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM