Based on the Multi-Tenancy Rails book.