Reputation: 581
This is the first time I'm creating an app using Ruby on Rails.
I would like to switch the database depending on the site which is loaded. With php I used to do a simple strpos on http host, and set the database name depending on that. How can I do this with rails?
Upvotes: 0
Views: 688
Reputation: 83680
You can run your application with different environments (i.e. domain1_production, domain2_production etc) so you can define in database.yml database environments for all your domains. But each environment have to run his own proccess in memory.
Another solution is to set before_filter in each model and parsing your domain set up database connection you require. But this is quite strange hack
Upvotes: 0
Reputation: 138032
When a Rails app starts in production mode, it preloads its classes and retrieves e.g. column data from the configured database. To use multiple databases, you would have to stop it doing this - and that could potentially cause a lot more problems than it solves.
A much easier to manage solution is simply to have two copies of the app, one listening on each domain.
A Rails app is generally designed to run backed by a single database, if you have data that is only specific to a single domain then your data should maybe all be in the same database, but have the relevant models linked to a particular "SiteDomain" model which you can then content-manage.
Upvotes: 2