Using Magic Multi-Connections

Posted by gregwinn

This plug-in is a great one! I have found a few bugs but it did the job just as i needed it to. I am using this plug-in to connect to three databases, where i am selecting, updating, inserting, and deleting. By adding this plug-in i was able to set my databases in the database.yml file and name them as i wanted. Next i adding two modules to my environment.rb file. After that i was able to connect to each of the databases with ease.

Below is how my database.yml file looks:


databaseone:
  adapter: mysql
  database: one
  username: root
  password: 
  host: localhost

databasetwo:
  adapter: mysql
  database: two
  username: root
  password: 
  host: localhost

production:
  adapter: mysql
  database: three
  username: root
  password: 
  host: localhost

Now in my environment.rb file i have:


module Dbone
  establish_connection :databaseone
end
module Dbtwo
  establish_connection :databasetwo
end

Now if i would like to select from my database one i do the following in any controller:


def index
  @users = Dbone::Users.find(:all)
end

Get the idea now? It's the same to update and add to the database. Just use the module name. Now for the "production" database everything remains the same, no need to add a module to it.

Get Magic Multi-Connections V1

Comments

Leave a response