Rails Rails 操作数据库如何用代码判断数据库是否连接上

Cger007 · January 17, 2018 · Last by ceclinux-github replied at January 17, 2018 · 1223 hits

如题

ActiveRecord::Base.connection.active?

Reply to gihnius

如何在 rails 配置 database.yml 中多个数据库的时候做到其中一个数据库连接不上,不会影响启动整个服务?现在的情况是报一个微不足道的数据库连接超时,可是这个数据库针对于整个 web 项目来说功能很小

Reply to Cger007

本来就不会有影响呀,你怎么配置的?

多个数据库,除了默认的,其他你要手工连接 establish_connection DB_CONFIG ,怎么会影响启动呢?

Reply to gihnius
-  production_report:
-  adapter: mysql2
-  database: workreport_clear
-  host: 192.168.113.120
-  username: ****
-  password: ****
-  encoding: utf8

- production_report_erp:
-  adapter: mysql2
-  database: workreport_erp
-  host: 192.168.113.120
-  username: root
-  password: ******
-  encoding: *****

- production_cardinfo:
-  adapter: postgresql
-  database: cmsdb
-  host: 192.168.111.112
-  username: postgres
-  password: ****
-  encoding: ****
  • 以上是 database.yml 配置

  • 这个是 model

    class MxCardinfoEvent < ActiveRecord::Base
    self.table_name = "ac_event"
    self.abstract_class = true
    establish_connection :production_cardinfo
    end
    
Reply to Cger007

这样不影响启动吧?

Reply to yfractal

兄弟能力有限啊,这个看不懂。。。

google rails test database connection

You need to Sign in before reply, if you don't have an account, please Sign up first.