Multiple databases in Rails

Viewed 51003

Can this be done? In a single application, that manages many projects with SQLite. What I want is to have a different database for each project my app is managing.. so multiple copies of an identically structured database, but with different data in them. I'll be choosing which copy to use base on params on the URI.

This is done for 1. security.. I'm a newbe in this kind of programming and I don't want it to happen that for some reason while working on a Project another one gets corrupted.. 2. easy backup and archive of old projects

9 Answers

What you've described in the question is multitenancy (identically structured databases with different data in each). The Apartment gem is great for this.

For the general question of multiple databases in Rails: ActiveRecord supports multiple databases, but Rails doesn’t provide a way to manage them. I recently created the Multiverse gem to address this.

The best solution I have found so far is this:

There are 3 database architectures that we can approach.

  • Single Database for Single Tenant
  • Separate Schema for Each Tenant
  • Shared Schema for Tenants

Note: they have certain pros and cons depends on your use case.

I got this from this Blog! Stands very helpful for me.

You can use the gem Apartment for rails

Video reference you may follow at Gorails for apartment

Related