简体   繁体   English

单个数据库Ruby on Rails 4具有多个用户

[英]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. 由于担心查询过多数据库,我进行了一些研究,发现了一些有关Rack和中间件的信息。 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. ** PS。 All commercial Customers will have unique URL's ex. 所有商业客户都将拥有唯一的URL ex。 customer1.myapp.com 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). 您正在构建一个标准的Rails应用程序,该应用程序可以服务于不同的客户端(用户,如果可以的话)。 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. 重要的是要确保查询效率高,并正确使用ActiveRecord系统,这将大大提高效率。 You have Model#includes , Model#eager_load , amongst other methods at your disposal. 您可以使用Model#includesModel#eager_load以及其他Model#eager_load方法。

As for customer1.myapp.com , there is a lot of information in Rails guides about getting those things set up and interfacing with it. 至于customer1.myapp.com ,Rails指南中有很多信息来进行设置和接口。

What you're looking for is Multitenancy . 您正在寻找的是Multitenancy

There are several approaches on how to implement it. 有几种方法可以实现它。 One of the options is to use PostgreSQL's schemas. 一种选择是使用PostgreSQL的模式。

You can take a look on the gem Apartment . 您可以看看宝石公寓 Maybe you can get some idea out of it. 也许您可以从中得到一些想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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