我正在开发一个多数据库 Rails 3 应用程序。每个数据库都有不同的模式(并且在生产中位于不同的位置)。我已将应用程序设置为与不同的数据库对话,如下所示:
数据库.yml
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: main_development
pool: 5
username: someuser
password: somepassword
socket: /tmp/mysql.sock
other_development:
adapter: mysql2
encoding: utf8
reconnect: false
database: other_development
pool: 5
username: someuser
password: somepassword
socket: /tmp/mysql.sock
模型/other_base.rb
class OtherBase < ActiveRecord::Base
self.abstract_class = true
establish_connection "other_#{Rails.env}"
end
模型/some_model.rb
class SomeModel < OtherBase
# Regular stuff here
end
现在,这适用于 web 应用程序,但不适用于运行 rake 任务,包括测试(未正确加载夹具)。有没有可用的宝石?任何帮助表示赞赏。
此外,最好创建一个 schema.rb 文件,该文件可以处理不同数据库的不同模式 - 也就是说,将允许我执行 rake db:create 或 db:setup 之类的操作,并让它使用数据库创建多个数据库- 特定模式。