我们有一个主数据库,我们所有的应用程序驻留。
We have a primary database where all of our app resides.
但有一个第二个数据库(从外部源更新),我想能够连接到所以我可以从它拉数据。我不需要写任何东西,只是阅读。
But there's a second database (updated from an external source), that I'd like to be able to connect to so I can pull data from it. I don't need to write anything...just read.
它也只有一个表,我从中拉。
It also only has one table that I'm pulling from.
我真的只需要做一些事情:
I really just need to do something like:
OtherDatabase.articles.where(id > 1000)就是这样。
如何在Rails中运行(运行3.2.13)?
So how can I do this in Rails (running 3.2.13)?
推荐答案对于简单的场景,Rails可以支持,宝石只需在database.yml中定义数据库:
For simple scenarios, Rails can support this without any extra gems; simply define the database in database.yml:
other_db: adapter: mysql2 encoding: utf8 database: other_db username: user password: passwd host: 1.2.3.4 port: 3306然后在模型中要使用其他数据库add:
Then in the model you want to use the other database add:
class Article < ActiveRecord::Base establish_connection(:other_db) self.table_name = 'other_db.articles' end然后你可以执行你的查询:
And then you can perform your query:
Article.where("id > 1000")=)
更多推荐
从第二个数据库拉取数据的有效方式?
发布评论