【问题标题】:Connecting to multiple databases in ruby on rails在 ruby​​ on rails 中连接到多个数据库
【发布时间】:2013-06-26 03:46:53
【问题描述】:

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

【问题讨论】:

标签: ruby-on-rails database connection


【解决方案1】:

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

config/database.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

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

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

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

类别模型

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

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

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

  def change
    # your code goes here.
  end
end

希望它对你有用:)。

【讨论】:

  • 在 Rails 5.1 上,我必须符号化连接名称。 establish_connection "#{Rails.env}_sec".to_sym
  • 没有符号化,我得到错误ActiveRecord::AdapterNotSpecified: database configuration does not specify adapter1.
【解决方案2】:

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

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

你也可以像这样从 database.yml 传递一个预配置的环境:

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

您也可以为特定型号设置它:

MyClass.establish_connection(...)

【讨论】:

  • 谢谢!连接到活动记录类之外的单独数据库需要ActiveRecord::Base.configurations['other_env'],否则它实际上不会加载配置。
【解决方案3】:

您可能需要注意,从 Rails 6(2019)开始,Rails 支持多个主数据库!

https://guides.rubyonrails.org/active_record_multiple_databases.html

database.yml 文件现在看起来像这样:

development:
  primary:
    database: primary_db
    user: root
  primary_replica:
    database: primary_db
    user: ro_user
    replica: true
  animals:
    database: my_animals_db
    user: root
    migrations_path: db/animals_migrate
  animals_replica:
    database: my_animals_db
    user: ro_user
    replica: true

然后就像在模型文件中指定一样简单:

class AnimalsModel < ApplicationRecord
  self.abstract_class = true
  connects_to database: { writing: :animals_primary, reading: :animals_replica }
end

class Dog < AnimalsModel
  # connected to both the animals_primary db for writing and the animals_replica for reading
end

(这些例子取自this helpful tutorial。)

【讨论】:

    猜你喜欢
    • 2011-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-12
    • 1970-01-01
    • 1970-01-01
    • 2021-09-30
    相关资源
    最近更新 更多