0

I would like to configure 2 database instances for a certain environment (say staging or production). The default rails new application just provides a single database instance how do I configure 2 database instances.

2
  • Do you mean that you want to use a different database for storing certain information? Or that you want to store all your data in two different databases? The former can be accomplished by adding more configurations to config/database.yml and using establish_connection in the class files (see pullmonkey.com/2008/4/21/…). The latter... I'm not actually sure how that would work. Commented Jun 25, 2012 at 17:10
  • I am trying to pull data from 2 different database instances, for different portions of the UI. There won't be any relationship between the 2 databases though. Commented Jun 25, 2012 at 17:20

1 Answer 1

1

You can copy and past one of your existing configurations such as development. and rename it as you want so, in the database.yml:

development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: 
  pool: 5
  username: root
  password: 
  host: localhost


new_database:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: 
  pool: 5
  username: root
  password: 
  host: localhost

Then in the models you create for this new connection add the following methods to the model, for example:

class Document < ActiveRecord::Base
  self.table_name = "document"   # this allows you to hide a non comforming table name behind the rails model it is NOT necessary for establish_connection to work
  self.establish_connection "new_database"  # notice there is no = when setting this value, strange, I know.
end
Sign up to request clarification or add additional context in comments.

Comments

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.