gpt4 book ai didi

ruby-on-rails - ActiveRecord::Base.establish_connection 与 AWS 上的 postgresql

转载 作者:行者123 更新时间:2023-11-29 13:56:18 26 4
gpt4 key购买 nike

当我像这样配置我的 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 上

最佳答案

这是工作代码:

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

关于ruby-on-rails - ActiveRecord::Base.establish_connection 与 AWS 上的 postgresql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31097749/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com