想要基于主键为两个数据库共享同一组 Active Record 模型
我有一组 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
现在我们正在研究用于数据库分片的 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
这是另一个解决方案。我还没有尝试过:
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.