5

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!

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

does this not work if you have two development: with different key/valuee?
0

does this not work if you have two development: with different key/valuee?

you have to add two development key like

  1. development
  2. development_sec

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.