Taken from "Multitenancy with Rails' by R. Bigg