简体   繁体   中英

Multiple users with single database Ruby on Rails 4

I'm building a system that (when completed) will serve multiple commercial customers. I need to accomplish the following:

  1. App should use one database
  2. Each commercial customer will have multiple users.
  3. When any of the commercial customer's users create/read data, they should do so under the commercial customer's namespace....Thus they should not be able to see other customers' data and should also not be allowed to create data etc. for other customers.

Initially I thought that the best way would be to assign all users to a commercial customer via association. As I was worried about querying the database too much, I did some research and found some info regarding Rack and middleware. This seems to be another option of achieving what I want, but it confused me a little.

What is the best option to achieve this sort of functionality in my app?

**PS. All commercial Customers will have unique URL's ex. customer1.myapp.com

Thanks

Rack and Middleware are layers that requests go through before they hit the code you wrote in your Controllers and Models. In short, if you need some preprocessing, or anything of that nature going on, then you shouldn't mess with middleware. For this particular case, you don't need to deal with middleware.

You're building a standard Rails app that serves different clients (users, if you will). It is okay to make many queries to the DB. The important thing is to make sure your queries are efficient and that you're making proper use of the ActiveRecord system, which helps a lot in efficiency. You have Model#includes , Model#eager_load , amongst other methods at your disposal.

As for customer1.myapp.com , there is a lot of information in Rails guides about getting those things set up and interfacing with it.

What you're looking for is Multitenancy .

There are several approaches on how to implement it. One of the options is to use PostgreSQL's schemas.

You can take a look on the gem Apartment . Maybe you can get some idea out of it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM