想要基于主键为两个数据库共享同一组 Active Record 模型

发布于 2024-11-09 09:33:52 字数 251 浏览 0 评论 0原文

我有一组 ActiveRecord 模型和两个数据库(具有相同的架构),我想使用它们。我想根据父模型的主键动态切换连接。另一种方法是复制模型集,并让它们从连接到另一个数据库的不同超类继承(http://pragdave.pragprog.com/pragdave/2006/01/sharing_externa.html)。基本上,我正在构建一个界面,允许用户比较两个数据库中的相似数据。然而,有时用户要比较的数据来自同一个数据库。该数据将是只读的,因为我不需要向任一数据库写入任何内容。

I have one set of ActiveRecord models and two databases (with the same schema) I want to use them against. I would like to switch the connection on the fly based on the primary key of the parent model. The alternative would be to duplicate the set of models and have them inherit from a different superclass whose connection is the other db (http://pragdave.pragprog.com/pragdave/2006/01/sharing_externa.html). Basically, I am building an interface which will allow users to compare similar data in the two databases. However, sometimes the data the users will be comparing will come from the same database. This data will be read-only as I don't need to write anything to either db.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半衾梦 2024-11-16 09:33:52

现在我们正在研究用于数据库分片的 octopus,也许这对您也有用?

https://github.com/tchandy/octopus

Right now we are looking into octopus for database sharding, maybe that could be useful for you too?

https://github.com/tchandy/octopus

七秒鱼° 2024-11-16 09:33:52

这是另一个解决方案。我还没有尝试过:

http://magicmodels.rubyforge.org/magic_multi_connections/

该页面承认1.0.0 版本中已发现问题。它也不像章鱼(在另一个答案中提到)那样“透明”,章鱼可以通过在现有代码周围包装一个块来使用不同的连接。

Here is another solution. I have not tried it:

http://magicmodels.rubyforge.org/magic_multi_connections/

The page admits that issues have been found in version 1.0.0. It also doesn't seem as "transparent" as octopus (mentioned in the other answer), which can use a different connection by wrapping a block around existing code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文