Rails:多个数据库,相同的模式 [英] Rails: Multiple databases, same schema

查看:141
本文介绍了Rails:多个数据库,相同的模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在虚构情景项目中,我允许公司的多个用户登录,创建记录等,谁都连接到一个数据库。

I'm in the middle of a fictional scenario project where I have allowed multiple users for a company to log in, create records, and so on, who all connect to the one database. They can all records absence records, attendance records, and so on.

然而,我想做的是使用这个相同的模式,但是扩展它允许多个公司使用相同的模式拥有自己的数据库。所以每个公司都有自己的数据,但所有公司使用相同的数据模型。换句话说,所有公司都可以创建缺席记录,但他们每个人只能访问自己创建的缺席记录。

What I want to do however, is use this same schema but expands this to allow several companies to have their own databases using the same schema. So each company will have their own data, but all companies use the same data model. In other words all company's can create absence records, but they each only have access to their own absence records that they created themselves.

我如何实现这一点?

我需要的是两个或三个文件,我不会商业化,如果你们认为我在别人的支出下陷入困境!

All I need is two or three files for this, I'm not going commercial with it in case you guys think I'm cutting corners at someone else's expense!

像if-else那样决定使用哪个文件对我来说非常有用,所以如果这样的代码存在,请让我知道。

Something as simple as an if-else that decides which file to use would be very useful to me, so if such a line of code exists please let me know.

推荐答案

我认为你做错了(除非你有一个很好的理由为每个公司有一个数据库),Coz看起来像你重复您的数据模型一遍又一遍,同时给您的代码带来不必要的复杂性。

I think you are doing it wrong (unless you have a really good reason to have a database for each company), Coz it seems like you are repeating your data model over and over while introducing unnecessary complexity to your code.

尝试让所有公司都在一个DB /表中,并且由comapny_id

Try to have all the companies in one DB/tables with having seperated by the comapny_id

Ex:data结构如下

Ex: data structure would be as follows

companies table
id
name


users table
id
user_name
company_id

如果您确实想要连接到多个数据库,请检查以下 SO问题

However if you really want to connect to multiple databases, check following SO question

这篇关于Rails:多个数据库,相同的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆