2

in my rails app, i want to connect to two database servers. one is ms-sql(microsoft sql server) and other is mysql database. how can i connect to both servers in parallel. i know some people use sqlite3 in development, and in production postgres database. i dont want in this way. what i want is either it is development or production i want to connect to both the databases in parallel. how can i? in what way i need to write the database.yml file. is there any gem for connecting to ms-sql server database?

default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: mysql-user-name
  password: mysql-password
  socket: /var/run/mysqld/mysqld.sock

  adapter: ms-sql like gem? 
  encoding: utf8
  pool: 5
  username: ms-sql-user-name
  password: ms-sql-password
  socket: /var/run/mysqld/mysqld.sock



  development:
  <<: *default
  database: template_development
John
  • 1,273
  • 3
  • 27
  • 61
  • 1
    You can find waht you are looking for here I think http://stackoverflow.com/questions/1825844/multiple-databases-in-rails – Hunter Killer Dec 31 '15 at 08:26

1 Answers1

1

I've done something similar, although there's a major problem when it comes to the likes of has_many :through:

#lib/parallel.rb
class Parallel < ActiveRecord::Base
   establish_connection "other_#{RAILS_ENV}"
end

#config/database.yml
development:
  adapter: mysql
  database: development
  username: root
  password:
  host: localhost

other_development:
  adapter: mysql
  database: otherdb_development
  username: root
  password:
  host: localhost

This allows you to inherit from the "other connection" db:

#app/models/user.rb
class User < Parallel
   ...
end

Although this works perfectly (albeit with some issues pertaining to the schema of the other db), you'll have a major issue when trying to use the likes of has_many :through.

I don't have any specifics, but you'd have to be careful with mixing your associations between db's with this functionality.

Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147