How to use 2 different database adapters (SQL Server and PostgreSQL) in Rails in the same environment?

末鹿安然 提交于 2019-12-07 20:16:45

问题


I'm working on a Rails 3 project which uses a PostgreSQL database.

I need to make queries on a remote SQL Server database sometimes (pure sql and stored procedures)

To do the trick I can use TinyTDS to access the SQL Server and execute queries:

development:
  adapter: sqlserver
  mode: dblib
  host: 123.123.123.123
  port: 1433
  database: the_db_name
  username: the_user
  password: the_pwd
  timeout: 5000

It's working well, but I don't know how to use the 2 different database adapters at the same time.

Is it possible? Or maybe there's an other way to do it?


回答1:


In your database.yml file add another "environment" block for your postgres connection

example environment name might be

postgres_development

Then in your environment.rb file add the following Line

[ModelName].establish_connection "postgres_#{RAILS_ENV}"

Where [ModelName] is the ActiveModel object you wish to specifically connect to the other database environment. I am not sure if this works in rails 3 100% but I know it works in rails 2



来源:https://stackoverflow.com/questions/8948913/how-to-use-2-different-database-adapters-sql-server-and-postgresql-in-rails-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!