开发者

One Rails model with two database choices, chosen on instantiation

My Rails app (let's call it "Mira") will be interfacing with an existing app (let's call it "Jira"). Mira will store information about Jira and will be able to directly manipulate its database (because Jira, we'll say, has an incomplete API).

Since I want to directly manipulate Jira's database, it makes sense to have models representing each of Jira's tables in my Mira app. That way I can use ActiveRecord to manipulate it.

But in fact! There are two Jiras. A Staging instance and a Production instance.

So now I want my model that was happily interfacing with one instance of Jira to be able to use a different database.

It would be super sweet if I could do this when I instantiate my model, perhaps like this:

Jira::C开发者_开发知识库ustomField.new(:staging)

or something like that.

Thoughts? Better ways to accomplish this? Is my goal as stated even possible?


As the documentation for ActiveRecord::Base discusses, it is easy to have different Rails model objects connecting to different databases using the establish_connection method.

However, if you want the same class to connect to multiple databases based on configuration, that will be kind of a pain. Do you need to use ActiveRecord here or could you use DataMapper? That would work better in this scenario I think. Check out What ORM to use in one process multiple db connections sinatra application? for an example

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜