2017-03-22 11 views
0

これは executing raw sqlのフォローアップです。私は、複数のデータベースから情報をストリーミングする必要があるプロジェクトに取り組んでいます。複数のデータベースに対してraw SQLを実行

sql = "Select * from ... your sql query here" 
records_array = ActiveRecord::Base.connection.execute(sql) 

と似たようなことをどうすればよいですか?接続の選択はサポートしていますか?

答えて

0

ActiveRecord::Base.establish_connectionを使用して、データベース接続を切り替えることができます。コードは次のようにする必要があります:

#database.yml 
development: 
    adapter: postgresql 
    host: 127.0.0.1 
    username: postgres 
    password: postgres 
    database: development_db 

development_another_db: 
    adapter: postgresql 
    host: 127.0.0.1 
    username: postgres 
    password: postgres 
    database: another_db 


ActiveRecord::Base.establish_connection :development_another_db 
sql = "Select * from ... your sql query here" 
records_array = ActiveRecord::Base.connection.execute(sql) 

ActiveRecord::Base.establish_connection :development 
sql = "Another select" 
records_array = ActiveRecord::Base.connection.execute(sql) 

あなたはRails documentationestablish_connectionについての詳細を見つけることができます。

関連する問題