在 ruby​​ on rails 中连接到多个数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17311199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:13:16  来源:igfitidea点击:

Connecting to multiple databases in ruby on rails

ruby-on-railsdatabaseconnection

提问by user1428970

I have a ruby on rails application working fine and connected to a database. Now i want to connect to a different database from the same application. The data model can be exactly the same. In fact if i connect to the different database the application works fine. However I want to connect to two different databases. Is it possible in ruby on rails?

我有一个 ruby​​ on rails 应用程序工作正常并连接到数据库。现在我想从同一个应用程序连接到不同的数据库。数据模型可以完全相同。事实上,如果我连接到不同的数据库,应用程序工作正常。但是我想连接到两个不同的数据库。在 ruby​​ on rails 中可能吗?

回答by Bachan Smruty

For multiple database connection, you need to add the following codes to the database.yml file. Here, I am giving the example of connecting two databases from a rails application

对于多数据库连接,需要在database.yml文件中添加如下代码。在这里,我给出了从 Rails 应用程序连接两个数据库的示例

config/database.yml

配置/数据库.yml

development:
  adapter: mysql2
  database: db1_dev
  username: root
  password: xyz
  host: localhost

development_sec:
  adapter: mysql2
  database: db2_dev
  username: root
  password: xyz
  host: localhost

production:
  adapter: mysql2
  database: db1_prod
  username: root
  password: xyz
  host: your-production-ip

production_sec:
  adapter: mysql2
  database: db2_prod
  username: root
  password: xyz
  host: your-production-ip

Here I have used two databases for the development and production environment.

这里我使用了两个数据库用于开发和生产环境。

Now we need to connect the model to databases. When you are running your application in development and production mode, all the models will be mapped through the development and production db parameters those been mentioned in your database.yml. So for some model we need to connect to other database.

现在我们需要将模型连接到数据库。当您在开发和生产模式下运行应用程序时,所有模型都将通过 database.yml 中提到的开发和生产数据库参数进行映射。所以对于某些模型,我们需要连接到其他数据库。

Lets assume that, we have two models User and Category. The users table is in db1_dev and db1_prod, the categories table in db2_dev and db2_prod.

让我们假设,我们有两个模型 User 和 Category。用户表在 db1_dev 和 db1_prod 中,类别表在 db2_dev 和 db2_prod 中。

Category model

品类模型

class Category < ActiveRecord::Base
  establish_connection "#{Rails.env}_sec"
end

Similarly, when you adding the new migration for the second database, need to add following code to it.

同样,当你为第二个数据库添加新的迁移时,需要添加以下代码。

class CreateRewards < ActiveRecord::Migration
  def connection
    ActiveRecord::Base.establish_connection("#{Rails.env}_sec").connection
  end

  def change
    # your code goes here.
  end
end

Hope it will work for you :) .

希望它对你有用:)。

回答by PinnyM

Use establish_connectionto switch to a different database:

使用establish_connection要切换到不同的数据库:

ActiveRecord::Base.establish_connection(
  :adapter  => "mysql",
  :host     => "localhost",
  :username => "myuser",
  :password => "mypass",
  :database => "somedatabase"
)

You can also pass a preconfigured environment from database.yml like so:

您还可以从 database.yml 传递预配置的环境,如下所示:

ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['other_env'])

You can also set it for a specific model:

您还可以为特定模型设置它:

MyClass.establish_connection(...)