# Reading from a separate database

**URL:** <https://www.ruby-forum.com/t/reading-from-a-separate-database/165079>\
**Category:** Rails\
**Created:** [April 8, 2009, 9:57pm UTC](https://www.ruby-forum.com/t/reading-from-a-separate-database/165079 "2009-04-08T21:57:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbaty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jbaty](https://www.ruby-forum.com/u/jbaty)\
**Post date:** [April 8, 2009, 9:57pm UTC](https://www.ruby-forum.com/t/reading-from-a-separate-database/165079/1 "2009-04-08T21:57:53Z")

</div>

Hi everyone,

I’m working on an app that needs to READ data from a separate database  
which is constantly being updated by a cron job. If anyone here is  
familiar with it, it’s a database populated by SNMP data from an open  
source project called RTG ([http://rtg.sourceforge.net/](http://rtg.sourceforge.net/))

What I need to do is connect to this database and grab the info. Now,  
the DB has a number of tables, each of which look something like this:

ifOutOctets\_1  
ifOutOctets\_2  
ifOutOctets\_3  
…  
ifInOctets\_1  
ifInOctets\_2  
ifInOctets\_3  
…  
ifOutUcastPkts\_1  
ifOutUcastPkts\_2  
ifOutUcastPkts\_3  
…  
ifInUcastPkts\_1  
ifInUcastPkts\_2  
ifInUcastPkts\_3  
…  
interface  
router

Those numbers can increase at any time so creating a model for each one  
seems like it won’t work out too well in the long run.

I started off by creating a model for each and establishing a connection  
to that DB with those, but like I said that probably won’t work out over  
time as more tables are added by the cron job outside the scope of the  
app. Is there some magical Rails way of making something amazingly great  
like… oh, I don’t know… Rtg.ifOutOctets\_1.find(:all)?

Haha, OK, something more realistic then?

---

<div class="post-metadata">

**Author:** ![jbaty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jbaty](https://www.ruby-forum.com/u/jbaty)\
**Post date:** [April 8, 2009, 10:01pm UTC](https://www.ruby-forum.com/t/reading-from-a-separate-database/165079/2 "2009-04-08T22:01:26Z")

</div>

Well, I woudn’t use it for your case, but here it is →   
[http://magicmodels.rubyforge.org/](http://magicmodels.rubyforge.org/)

But i think going into straight SQL from a activerecord connection  
object might be a lot simpler.

- 

Maurício Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)

On Wed, Apr 8, 2009 at 4:57 PM, Jack B.

---

<div class="post-metadata">

**Author:** ![jbaty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jbaty](https://www.ruby-forum.com/u/jbaty)\
**Post date:** [April 8, 2009, 10:48pm UTC](https://www.ruby-forum.com/t/reading-from-a-separate-database/165079/3 "2009-04-08T22:48:55Z")

</div>

That actually seems to work, but why do you think I shouldn’t use it?

---

<div class="post-metadata">

**Author:** ![jbaty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jbaty](https://www.ruby-forum.com/u/jbaty)\
**Post date:** [April 8, 2009, 11:20pm UTC](https://www.ruby-forum.com/t/reading-from-a-separate-database/165079/4 "2009-04-08T23:20:34Z")

</div>

"cos you would still fall into the ifInUcastPkts\_3, ifInUcastPkts\_2  
and ifInUcastPkts\_1models problem.

Doing it with straight SQL, you could just query the database about  
which tables are available and read them. A simple “show tables” query  
would to the trick on MySQL for example.

- 

Maurício Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)

On Wed, Apr 8, 2009 at 5:48 PM, Jack B.
