Jakub
Jakub

Reputation: 739

One model - many DB connections in Rails?

(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!

Upvotes: 1

Views: 155

Answers (2)

tadman
tadman

Reputation: 211700

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.

Upvotes: 1

Mick
Mick

Reputation: 5217

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

Upvotes: 2

Related Questions