0

我想为某个环境(例如登台或生产)配置 2 个数据库实例。默认的 rails new 应用程序只提供一个数据库实例,我该如何配置 2 个数据库实例。

4

1 回答 1

1

您可以复制并粘贴现有配置之一,例如开发。并根据需要在 database.yml 中重命名它:

development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: 
  pool: 5
  username: root
  password: 
  host: localhost


new_database:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: 
  pool: 5
  username: root
  password: 
  host: localhost

然后在您为此新连接创建的模型中,将以下方法添加到模型中,例如:

class Document < ActiveRecord::Base
  self.table_name = "document"   # this allows you to hide a non comforming table name behind the rails model it is NOT necessary for establish_connection to work
  self.establish_connection "new_database"  # notice there is no = when setting this value, strange, I know.
end
于 2012-06-25T17:22:27.227 回答