3

I have an app deployed in heroku that uses postgresql as database. I have created a new model that will connect to a remote mysql database like the following:

# other_database.rb(apps/models/)
class OtherDatabase < ActiveRecord::Base
  self.abstract_class = true
  establish_connection "remote_#{Rails.env}"
end

# other_model.rb(apps/models/)
class OtherModel < OtherDatabase
  self.table_name = "users"
end

i have also edited the database.yml file and added these entries:

remote_development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: app_development
  pool: 5
  username: root
  password:
  socket: /var/run/mysqld/mysqld.sock

remote_production:
  adapter: mysql2
  encoding: utf8
  reconnect: true
  database: app_production
  pool: 40
  username: root
  password: secretpassword
  socket: /var/run/mysqld/mysqld.sock
  host: 12.34.56.78

The remote database uses mysql as its database.

It works fine in my local machine, but when I deploy it to heroku, it creates an error page so I looked up on the heroku logs, and I found this one out:

/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0.beta1/lib/active_record/connection_adapters/connection_specification.rb:52:in `resolve_hash_connection': database configuration does not specify adapter (ActiveRecord::AdapterNotSpecified)
from /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.0.beta1/lib/active_record/connection_adapters/connection_specification.rb:46:in `resolve_string_connection'

I am spent almost all my time searching for some answers but to no avail. Hope anyone can help out with this issue.

TIA.

Eralph

4

1 回答 1

3

Heroku 删除并重新创建 database.yml 文件。这意味着你在 database.yml 文件中的任何内容都将被 heroku 完全忽略。

在您的建立连接方法调用中弹出您的数据库凭据,如下所示:

establish_connection(
  adapter: 'mysql2',
  encoding: 'utf8',
  reconnect: true,
  database: 'app_production',
  pool: 40,
  username: 'root',
  password: 'secretpassword',
  socket: '/var/run/mysqld/mysqld.sock',
  host: '12.34.56.78'
)

但是,最好使用数据库 URL 并将其存储在 heroku 的环境变量中。

heroku config:set MYSQL_DB_URL=http://example.com/rest_of_the_url

然后使用

establish_connection(ENV['MYSQL_DB_URL'])

可以在此处找到 URL 的示例和文档:http: //forums.mysql.com/read.php?39,10734,10750

于 2013-06-21T09:38:59.160 回答