Based on Multitenancy with Rails by @ryanbigg