views:

41

answers:

2

(using Ruby on Rails and ActiveRecord)

In short: Is there some way how a model can use different DB schema for each request?

Why I need id: I have a working intranet application working for one company but I decided I would like to offer it to other companies. The Rails way would be to add company_id to each model and alway use this scope. But making separate DB schema for each company would make much more sense. Is there some standard way how to do it?

thanks!

+2  A: 

What would be wrong with having a separate instance of your application for each company?

Mick Sharpe
I would like to offer free registration for anyone from the web and there are many flaws with that approach. App would have to handle deployment, malicious attack would create new process just by registering new user... just crazy problems
Jakub
+1  A: 

Adding company_id to all the models is absolutely the way to go. What you're talking about is very difficult to manage in the long haul, and it may be tricky to ensure the correct connection is used to store the correct data.

Although layering in differentiation like that is annoying, it can be done and proven in a fairly short period of time, and after that things will be easier to manage. With named_scope it is not hard to filter using attributes like that.

The simple alternative is to deploy the application more than once, with a different database.yml for each company, where the data is isolated on the application level, not within the application.

It would be easy to do this with Passenger (mod_rails) and a bit of shell scripting.

tadman
OK, I will take your word for it. This site is not intended for discussions anyway. Thanks for you answer!
Jakub