One approach is to use postgres's dblink.
From the docs:
dblink executes a query (usually a SELECT, but it can be any SQL
statement that returns rows) in a remote database.
First, you'll need to enable dblink in your database with:
CREATE EXTENSION dblink;
Then, in your application, you can execute a raw query statement with ActiveRecord::Base.connection.execute.
I created this test setup here:
- Database
db1 contains a table tbl with field1 and field2
columns.
- Database
db2 contains a table tbl with field1 and
field2 columns.
Both have 5 rows.
My database.yml:
development:
adapter: postgresql
encoding: unicode
database: db1
From rails console:
ActiveRecord::Base.connection.execute("
SELECT * FROM tbl
UNION ALL
SELECT * FROM dblink('dbname=db2','SELECT * FROM tbl') AS tbl2(field1 varchar, field2 int);
").to_a
# [{"field1"=>"one", "field2"=>1}, {"field1"=>"two", "field2"=>2}, {"field1"=>"three", "field2"=>3}, {"field1"=>"four", "field2"=>4}, {"field1"=>"five", "field2"=>5}, {"field1"=>"one", "field2"=>1}, {"field1"=>"two", "field2"=>2}, {"field1"=>"three", "field2"=>3}, {"field1"=>"four", "field2"=>4}, {"field1"=>"five", "field2"=>5}]
It's just a possible approach. You can setup dblink connection string to point to a remote server (for instance: 'dbname=yourdb port=5432 host=yourhost user=youruser password=yourpwd)
Be advised that this is not the rails way. Raw query is not related to a model. I suggest you to choose this only for specific tasks, like running a report.
EDIT
If you want one query for each database, and don't want to link it to your model, you can use ActiveRecord::Base.establish_connection, like this:
conn1 = {
adapter: 'postgresql',
encoding: 'utf8',
database: 'db1'
}
conn2 = {
adapter: 'postgresql',
encoding: 'utf8',
database: 'db1'
#, more config here - other host, for instance #
}
arr1 = ActiveRecord::Base.establish_connection(conn1).connection.execute("select * from tbl").to_a
# => [{"field1"=>"one", "field2"=>1}, {"field1"=>"two", "field2"=>2}, {"field1"=>"three", "field2"=>3}, {"field1"=>"four", "field2"=>4}, {"field1"=>"five", "field2"=>5}] => [{"field1"=>"one", "field2"=>1}, {"field1"=>"two", "field2"=>2}, {"field1"=>"three", "field2"=>3}, {"field1"=>"four", "field2"=>4}, {"field1"=>"five", "field2"=>5}]
arr2 = ActiveRecord::Base.establish_connection(conn2).connection.execute("select * from tbl").to_a
# => [{"field1"=>"one", "field2"=>1}, {"field1"=>"two", "field2"=>2}, {"field1"=>"three", "field2"=>3}, {"field1"=>"four", "field2"=>4}, {"field1"=>"five", "field2"=>5}] => [{"field1"=>"one", "field2"=>1}, {"field1"=>"two", "field2"=>2}, {"field1"=>"three", "field2"=>3}, {"field1"=>"four", "field2"=>4}, {"field1"=>"five", "field2"=>5}]
You'll get two arrays arr1 and arr2 with both data.