I need to use different database connections in different Rails models. Is there a not-so-hacky way to do that?
Any links or search keywords would be great :)
This can be done several times to connect to different databases, with the restriction that it will only allow one connection to the same database. If you try to use a database from multiple instances of the same application either on the same computer or on different computers you will receive an error message.
so, based on user login, the application should connect different database server. For Ex: if user "xxx" login with credential and belogs to "ABC" company and the database is "ABC", then ABC data need to display on the web page.
1. Create a linked server in DB invironment, then create a SP to take care of it. 2. Get two DataSets for them, then merge two datatables into one based on usersID.
Add new sections to your database.yml
e.g.
other_development: adapter: mysql database: otherdb_development username: root password: host: localhost other_production: adapter: mysql database: otherdb_production username: root password: host: localhost
Add a class in lib/other_database.rb
class OtherDatabase < ActiveRecord::Base establish_connection "other_#{RAILS_ENV}" end
and then for each model which isn't in the default database subclass from OtherDatabase
e.g.:
class MyModel < OtherDatabase # my model code... end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With