蛇腹コードはdatabase.ymlの中で言及した特定のデータベース接続中に存在するすべてのテーブルの配列を返します。 other_connection_nameはdatabase.ymlので述べたDBへの接続の名前である
ActiveRecord::Base.establish_connection(Rails.configuration.database_configuration["#{other_connection_name}"]).connection.tables
。例えば
database.ymlを我々はdev_schemaとgbl_schemaに存在するすべてのテーブルを取得するには以下のコードを使用し上記の場合
development:
adapter: mysql2
encoding: utf8
collation: utf8_bin
reconnect: true
database: dev_schema
pool: 5
username: xxx
password: xxxx
host: localhost
otherconnection:
adapter: mysql2
encoding: utf8
collation: utf8_bin
reconnect: true
database: gbl_schema
pool: 5
username: xxx
password: xxx
host: localhost
database.ymlをそれぞれ
ActiveRecord::Base.establish_connection(Rails.configuration.database_configuration["development"]).connection.tables
ActiveRecord::Base.establish_connection(Rails.configuration.database_configuration["otherconnection"]).connection.tables
感謝の返信のためには、私はエラーが発生しました..ActiveRecord :: Base.establish_connection( "#{開発_秒}")。connection.tables NameError:un定義済みのローカル変数またはメソッド 'development_sec 'for main:Object –
database.ymlファイルにデータベース設定を追加しましたか? – puneet18
は、これは私のdatabase.ymlファイルのdevelopment_secです: モード:DBLIB アダプタ:SQLServerの データサーバ:123.201。**。*** データベース:。ZKA ユーザ名:saの パスワードは:私の更新の答えをチェックし –