问题
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