从第二个数据库拉取数据的有效方式? [英] Efficient way to pull data from second database?

查看:172
本文介绍了从第二个数据库拉取数据的有效方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个主数据库,我们所有的应用程序驻留。

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")

=)

这篇关于从第二个数据库拉取数据的有效方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆