开发者

Multiple domains on a single rails app

This is the first time I'm cr开发者_C百科eating 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?


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜