从第二个数据库拉取数据的有效方式?

编程入门 行业动态 更新时间:2024-10-28 15:20:10
本文介绍了从第二个数据库拉取数据的有效方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

=)

更多推荐

从第二个数据库拉取数据的有效方式?

本文发布于:2023-10-22 20:04:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1518568.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:第二个   方式   数据库   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!