连接的Rails 3.1与多个数据库 [英] Connecting Rails 3.1 with Multiple Databases

查看:248
本文介绍了连接的Rails 3.1与多个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前ShowNearby我们一直在做从PHP非常大的迁移到回报率3.1,我们所面临的一些问题,可能是一些你之前已经解决了。

At ShowNearby we have been doing a very big migration to RoR 3.1 from PHP and we are facing several problems that may be some of you have solved before.

我们有大数据量,我们决定分开我们的数据库分成几个数据块,我们可以分开处理。例如,我们的帐户,地点,日志和其他人被分成几个数据库

We have big amounts of data and we decided to segregate our DB into several DBs that we can handle separately. For example, our accounts, places, logs and others are split into several databases

我们需要得到迁移,夹具,模型,很好地发挥,而且到目前为止,它已经相当凌乱。我们的一些解决的办法要求,是可以接受的:

We need to get migrations, fixtures, models, to play nicely, and so far it has been quite messy. Some of our requirements for a solution to be acceptable:

  • 在一个模型应该涉及一个表中的数据库之一。
  • 耙分贝:滴 - 应该放弃所有的数据库ENV我们在database.yml中
  • 指定
  • 耙分贝:创建 - 应该创建所有的数据库ENV我们在database.yml中
  • 指定
  • 耙分贝:迁移 - 应该运行迁移到不同的数据库
  • 耙分贝:测试 - 应该抓住装置和拖放到各种数据库和测试单元/功能/等

我们正在考虑按每个数据库设置单独的轨项目,并与连接的ActiveResource他们,但我们觉得这不是很有效。之前有任何的你处理过类似的问题?

We are considering setting separate rails projects per each database and connecting them with ActiveResource, but we feel this is not very efficient. Have any of you deal with a similar problem before?

感谢这么多!

推荐答案

要Wukerplank的回答,你也可以把连接的详细信息在database.yml中像往常一样,像这样一个名字:

To Wukerplank's answer, you can also put the connection details in database.yml like usual with a name like so:

log_database_production:
  adapter: mysql
  host: other_host
  username: logmein
  password: supersecret
  database: logs

然后在您的特殊型号:

Then in your special model:

class AccessLog < ActiveRecord::Base
  establish_connection "log_database_#{Rails.env}".to_sym
end

要保留那些讨厌的凭据在应用程序的code是。

To keep those pesky credentials from being in your application code.

编辑:如果您要重新,多模式这一点上,你应该创建一个新的抽象类,从它继承,因为连接是紧耦合的类(如解释的here ,的这里和<一href="http://ilikestuffblog.com/2012/09/21/establishing-a-connection-to-a-non-default-database-in-rails-3-2-2/">here),而新的连接会为每个类创建。

If you want to reuse this connection in multiple models, you should create a new abstract class and inherit from it, because connections are tightly coupled to classes (as explained here, here, and here), and new connections will be created for each class.

如果是这样的话,做一些事情,像这样:

If that is the case, set things up like so:

class LogDatabase < ActiveRecord::Base
  self.abstract_class = true
  establish_connection "log_database_#{Rails.env}".to_sym
end

class AccessLog < LogDatabase
end

class CheckoutLog < LogDatabase
end

这篇关于连接的Rails 3.1与多个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆