Want to share the same set of Active Record models for two databases based on primary key
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_exter开发者_C百科na.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.
Right now we are looking into octopus
for database sharding, maybe that could be useful for you too?
https://github.com/tchandy/octopus
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.
精彩评论