与多个数据库具有并属于许多关系 [英] Has and belongs to many relationship with multiple databases

查看:62
本文介绍了与多个数据库具有并属于许多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这种情况下,我有两个模型,公司和权限,公司与我的权限数据库位于不同的数据库中。这是一个拥有并属于许多关系的关系,因为每个公司可以拥有许多许可权,而每个许可权可以属于许多公司。

I have a situation where I have two models, companies and permissions, where companies is in a separate database from my permissions database. This is a has and belongs to many relationship because each company can have many permissions and each permission can belong to many companies.

两个数据库分开的原因是因为公司数据库运行一个需求量很大的生产应用程序,而权限数据库控制另一个应用程序的权限。

The reason the two databases are split is because the company database runs a high demand production application and the permissions database controls the permissions for another application.

使用rails,它在与主数据库相同的数据库中查找联接表。表。例如,如果我执行company.permissions,它将在公司数据库中查找company_permissions。如果我执行permission.companies,它会在权限数据库中查找。

With rails, it looks for the join table in the same database as the primary table. For instance, if I do company.permissions, it looks in the company database for company_permissions. If I do permission.companies it looks in the permissions database.

使用has并属于多个数据库的许多关系的最佳解决方案是什么?

What is the best solution to using a has and belongs to many relationship with multiple databases?

推荐答案

拥有并属于许多旧的,笨拙的和有问题的。我建议改为使用has_many。
您可以使用:through选项指定关系表。只需选择您希望其驻留在哪个数据库中并创建一个模型来表示它即可。
http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/has_many

Has and belongs to many is old, clunky, and problematic. I recommend using has_many instead. You can specify the relationship table with the ":through" option; just pick which database you want it to reside in and create a model to represent it. http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/has_many

这篇关于与多个数据库具有并属于许多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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