如何在不使用任何 yaml 文件的情况下设置第二个数据库连接?

发布于 2024-10-18 06:43:04 字数 492 浏览 2 评论 0原文

我有一个在 Heroku 上运行的应用程序,我想建立与第二个数据库的连接(从 Heroku 上运行的另一个应用程序)。我见过的针对多个数据库的所有解决方案都涉及database.yml 文件,但Heroku 不会以这种方式执行操作,他们指示我在一个应用程序中使用另一个应用程序中的DATABASE_URL

我想我需要做类似的事情:

DatabaseName::Base.establish_connection(DATABASE_URL)

然后我可以

establish_connection :DatabaseName

在适当的模型中使用。我应该把

DatabaseName::Base.establish_connection(DATABASE_URL)

它放在哪里才能适用于所有型号?环境.rb?它的正确语法是什么?

I have an app running on Heroku and I want to set up a connection to a second database (from another app running on Heroku). All solutions I have seen for multiple DBs involve the database.yml file, but Heroku does not do things this way, they instructed me to use DATABASE_URL from one app in the other.

I think I need to do something like:

DatabaseName::Base.establish_connection(DATABASE_URL)

and then I can use

establish_connection :DatabaseName

in the appropriate models. Where do I put

DatabaseName::Base.establish_connection(DATABASE_URL)

to have it available to all models? environment.rb? And what is the correct syntax for it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遇到 2024-10-25 06:43:04

假设您有两个要连接的数据库DBOne和DBTwo(请相应填写数据)。希望有帮助。

   class DBOne < ActiveRecord::Base
    self.abstract_class = true
    establish_connection(
      :adapter  => "mysql",
      :host     => "hostname",
      :username => "myuser",
      :password => "mypass",
      :database => "database_one"
    )
    end

    class ModelInDbOne <DBOne
    end

    class DBTwo < ActiveRecord::Base
    self.abstract_class = true
    establish_connection(
      :adapter  => "mysql",
      :host     => "hostname",
      :username => "myuser",
      :password => "mypass",
      :database => "database_two"
    )
    end 

    class ModelInDbTwo < DBTwo
    end

Assuming you have two dbs DBOne and DBTwo which you want to connect (please fill up the data accordingly). Hope that helps.

   class DBOne < ActiveRecord::Base
    self.abstract_class = true
    establish_connection(
      :adapter  => "mysql",
      :host     => "hostname",
      :username => "myuser",
      :password => "mypass",
      :database => "database_one"
    )
    end

    class ModelInDbOne <DBOne
    end

    class DBTwo < ActiveRecord::Base
    self.abstract_class = true
    establish_connection(
      :adapter  => "mysql",
      :host     => "hostname",
      :username => "myuser",
      :password => "mypass",
      :database => "database_two"
    )
    end 

    class ModelInDbTwo < DBTwo
    end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文