Several DB joints in rails

I am trying to connect multiple databases in a ROR application. My .yml database looks like this: in database.yml

development:

 adapter: mysql
 username: root
 password: 
 database: example_development

private:

adapter: mysql
username: root
password: 
database: example_private_development

You can connect using connection_connection: private

I doubt that using rake db: create.I cannot get the solution from google.

Please help me clean it.

+5
source share
3 answers

Try

rake db:create:all

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

This is what I did once, I created two classes that inherit from ActiveRecord::Baseand establish connections inside these classes.

ActiveRecord

:

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

test1 test2:

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

:

class School < Test1Base
  #code
end

class Student < Test2Base
  #code
end
+6

.

,

db:migrate RAILS_ENV="portal_development"'.

DB.check

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

.

+2

Is it possible to use active_delegate? http://railslodge.com/plugins/595-active-delegate

0
source

All Articles