Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple DB connection in rails

I am trying to connect multiple database in ROR application.My database.yml is look like this in your database.yml file

development:

 adapter: mysql
 username: root
 password: 
 database: example_development

private:

adapter: mysql
username: root
password: 
database: example_private_development

It is possible to connect using establish_connection :private

My doubt is that how use rake db:create.I am not able get solution from google.

Please help me to clear it.

like image 403
Shamith c Avatar asked Sep 14 '11 14:09

Shamith c


People also ask

Can rails connect to multiple databases?

Rails now has support for multiple databases so you don't have to store your data all in one place. At this time the following features are supported: Multiple writer databases and a replica for each. Automatic connection switching for the model you're working with.

What is ActiveRecord in Ruby on rails?

What is ActiveRecord? ActiveRecord is an ORM. It's a layer of Ruby code that runs between your database and your logic code. When you need to make changes to the database, you'll write Ruby code, and then run "migrations" which makes the actual changes to the database.

What is ActiveRecord base?

ActiveRecord::Base indicates that the ActiveRecord class or module has a static inner class called Base that you're extending.


2 Answers

Try

rake db:create:all

And yes, it's possible to have multiple db connections in a Rails application.

This is what I did once, I have created two classes which inherit from ActiveRecord::Base and set the connections inside those classes.

Then I inherited all my models in one of those classes instead of direct ActiveRecord

Below is an example:

database.yml file

#app uses two database
#1 - test1
#2 - test2
test1:
  adapter: mysql
  encoding: utf8
  database: test1
  username: root 
  password: xxx
  host: localhost

test2:
  adapter: mysql
  encoding: utf8
  database: test2
  username: root
  password: xxx
  host: localhost

Then I have two models for both test1 and test2 databases:

class Test1Base < ActiveRecord::Base
    self.abstract_class = true
    establish_connection("test1")
end

class Test2Base < ActiveRecord::Base
  # No corresponding table in the DB.
  self.abstract_class = true
  establish_connection("test2")
end

Then I inherit my models according to database:

class School < Test1Base
  #code
end

class Student < Test2Base
  #code
end
like image 199
sameera207 Avatar answered Sep 24 '22 06:09

sameera207


Thanks for reply.

we can migrate a model for particular DB, for example

db:migrate RAILS_ENV="portal_development"'.

And more change for establishing connection with DB.check the corrected below

class Test1Base < ActiveRecord::Base
  self.abstract_class = true
  establish_connection :development
end

class Test2Base < ActiveRecord::Base
  # No corresponding table in the DB.
  self.abstract_class = true
  establish_connection :portal_development
end

Thanks sameera for your valuable reply.

cheers

Shamith c

like image 23
Shamith c Avatar answered Sep 21 '22 06:09

Shamith c