We have a primary database where all of our app resides.
But there's a second database (updated from an external source), that I'd like to be able to connect to so I can pull data from it. I don't need to write anything...just read.
It also only has one table that I'm pulling from.
I really just need to do something like:
OtherDatabase.articles.where(id > 1000)
And that's it.
So how can I do this in Rails (running 3.2.13)?
For simple scenarios, Rails can support this without any extra gems; simply define the database in database.yml:
other_db:
adapter: mysql2
encoding: utf8
database: other_db
username: user
password: passwd
host: 1.2.3.4
port: 3306
Then in the model you want to use the other database add:
class Article < ActiveRecord::Base
establish_connection(:other_db)
self.table_name = 'other_db.articles'
end
And then you can perform your query:
Article.where("id > 1000")
=)