2017-11-28 4 views
1

私の場合は。 私は1つのアプリケーションで2つの異なるデータベースを使用しています。 1つはmysqlで、もう1つはsqlseverです。このコマンドを使用するのActiveRecord :: Base.connection.tablesレールコンソールを使用して特定のデータベースのテーブルを表示することはできません

は、私は私のMySQLデータベーステーブル内のすべての表を参照してください持っています。 しかし、私はレールのコンソールを使用して特定のデータベーステーブルを表示するコマンドを知らない。コード下

答えて

0

試行:

a=ActiveRecord::Base.establish_connection(
    :adapter=> "adapter_name", 
    :encoding=> "utf8", 
    :reconnect=> "false", 
    :database=> "db_name", 
    :pool=> "5", 
    :username=> "xxxx", 
    :password=> "xxxxxxx", 
    :host=>"xx.xx.xx.xx" 
) 

a.connection.tables 
+0

感謝の返信のためには、私はエラーが発生しました..ActiveRecord :: Base.establish_connection( "#{開発_秒}")。connection.tables NameError:un定義済みのローカル変数またはメソッド 'development_sec 'for main:Object –

+0

database.ymlファイルにデータベース設定を追加しましたか? – puneet18

+0

は、これは私のdatabase.ymlファイルのdevelopment_secです: モード:DBLIB アダプタ:SQLServerの データサーバ:123.201。**。*** データベース:。ZKA ユーザ名:saの パスワードは:私の更新の答えをチェックし –

0

蛇腹コードはdatabase.ymlの中で言及した特定のデータベース接続中に存在するすべてのテーブルの配列を返します。 other_connection_nameはdatabase.ymlので述べたDBへの接続の名前である

ActiveRecord::Base.establish_connection(Rails.configuration.database_configuration["#{other_connection_name}"]).connection.tables 

。例えば

database.ymlを我々はdev_schemagbl_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 
関連する問題