5

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.

0

3 Answers 3

6

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
Sign up to request clarification or add additional context in comments.

2 Comments

Hi sameera , your answer is correct. i have one more question how can migrate models for "portal_development".Help me ...
Hi @Shamith, Oops, that was a typo, That should be the env names, I have edited the database.yml above, Since I used my existing file as an example forgot to remove some lines, sorry... :D
2

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

Comments

0

Possibly use active_delegate? http://railslodge.com/plugins/595-active-delegate

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.