How to Use Multiple Databases in a Rails App Using the database.yml
database, ruby-on-rails
Solution
I tried to replicate and got it to work. It has to do with naming conventions: You're in development mode and AR will look for the development tag, which in your case does not exist for sqlite.
Here is my database.yml:
development:
adapter: mysql2
database: se_development
username: root
pool: 5
timeout: 5000
sqlite_development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
Here is the model:
class Plot < ActiveRecord::Base
establish_connection 'sqlite_' + Rails.env
end
Worked for me. Hope this helps.
Problem
I've read up on the documentation on how to do this, but in practice, I am having problems. In my app, I have 2 different databases as described below in my database.yml file. ``` sqlite_test: adapter: sqlite3 database: db/sqlite_test.sqlite3 table: plots pool: 5 timeout: 5000 development: adapter: mysql2 encoding: utf8 reconnect: false database: test pool: 5 username: myname password: mypassword host: localhost ``` My application is a dynamic plotter that will plot the data in a (basic) database without having knowledge of whats in the database, or how its structured. Both of these databases contain different data. The SQLite database I created in a separate Rails app. The current app I'm using is built around the MYSQL database, which I build externally. I copied the SQLite database into the /db directory. So in my main model, when I say: ``` class Plot < ActiveRecord::Base establish_connection :development set_table_name "stock_test" set_primary_key :id ``` Everything works out just fine and dandy. However, when I change it to: ``` establish_connection :sqlite_test set_table_name "plots" ``` and try to access that database via the Rails console, I get an error saying: ``` >>ActiveRecord::AdapterNotSpecified: database configuration does not specify adapter ``` I don't know why that is, since the database.yml file clearly does specify an adapter? When I do it by hand in my model though, everything works exactly as it should. ``` class Plot < ActiveRecord::Base establish_connection(:adapter => "sqlite3", :database => "db/sqlite_test.sqlite3", :pool => 5 ) ``` Why does it all work when I manually specify whats in the database.yml, but not when I just use the database.yml reference? Thanks!