# Legacy DB with multiple tables per model

**URL:** <https://www.ruby-forum.com/t/legacy-db-with-multiple-tables-per-model/120275>\
**Category:** Rails\
**Created:** [November 15, 2007, 1:39pm UTC](https://www.ruby-forum.com/t/legacy-db-with-multiple-tables-per-model/120275 "2007-11-15T13:39:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kimmoke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e19adc/32.png) [@kimmoke](https://www.ruby-forum.com/u/kimmoke)\
**Post date:** [November 15, 2007, 1:39pm UTC](https://www.ruby-forum.com/t/legacy-db-with-multiple-tables-per-model/120275/1 "2007-11-15T13:39:09Z")

</div>

I’m working with a large DB that is also being used by another  
application(s) so I really can’t change it. I’d like to wrap it in  
ActiveRecord (or DataMapper/OG/Other ORM) models but it’s quite a  
challenge because models (in db) have so many parameters that they have  
been split across multiple tables. Views (on db) don’t come into  
question since that would create huge joins and fetch far to much data  
at once.

The DB structure probably has been generated by a program so it follows  
some sort of logic, but I’m ok with the idea that I’m going to have to  
map the thing once manually.

Another annoyance is the fact that the columns in the extra parameter  
tables are prefixed with a short form of the table name, of course I’d  
like to access them by the unprefixed name (there aren’t duplicate  
names) since I don’t want to keep looking up the table number where  
certain parameters are located (to use Ci.ipv6address instead of  
Ci.xcit4\_ipv6address)

Here’s an example model of ConfigurationItem in the DB (I’ve only picked  
several tables and parameters for clarity):

CDM\_CONFIGURATION\_ITEMS

- CIT\_OID
- CIT\_CPU\_OID
- CIT\_NAME1
- CIT\_NAME2

CDM\_CONFIGURATION\_ITEMS\_X

- XCIT\_CIT\_OID
- XCIT\_PURCHASEDATE
- XCIT\_CINUMBER1

CDM\_CONFIGURATION\_ITEMS\_X\_SD\_1

- XCIS1\_CIT\_OID
- XCIS1\_CITEXT91
- XCIS1\_IPADDRESS

CDM\_CONFIGURATIO\_ITEMS\_X\_SD\_10 (n dropped from configuration when going  
over 9)

- XCISA\_CIT\_OID (notice, after 9 comes A)
- XCISA\_SHORTTEXT36

CDM\_CONFIGURATIO\_ITEMS\_X\_SD\_18

- XCISI\_CIT\_OID
- XCISI\_SERIALNUMBER

I’d like to do something like \<%=  
ConfigurationItem.find(:first).ipadress %\> which would on demand make a  
query that gets the value (or eager load all values) from  
CDM\_CONFIGURATION\_ITEMS\_X\_SD\_1.

Also it would be great to do something like @cis =  
ConfigurationItem.find(:all, :include\_fields =\> [:citext91, :name2])  
that would magically create joins for the required tables.

I won’t do any writing through the DB connection, all of it is done via  
a Java API which I have been using through JRuby successfully, but  
that’s another story.

A colleague of mine already did this in lisp and of course I’d like to  
show that ruby isn’t bad either.

Ideas? Discuss 🙂

---

<div class="post-metadata">

**Author:** ![kimmoke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e19adc/32.png) [@kimmoke](https://www.ruby-forum.com/u/kimmoke)\
**Post date:** [November 15, 2007, 9:13pm UTC](https://www.ruby-forum.com/t/legacy-db-with-multiple-tables-per-model/120275/2 "2007-11-15T21:13:40Z")

</div>

On Nov 15, 7:39 am, Kimmo L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:  
[http://groups.google.com/group/rubyonrails-talk/browse\_thread/thread/f1d4421eb73b2d9c](http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/f1d4421eb73b2d9c)

Well, I suppose that if you have time, you can build up some network  
of Ruby objects that represent the data schema the way it is in the  
database, and correspondingly, this data structure could also  
represent the schema the way you would like to view it from the object  
world, and it could represent the correspondence between the two.  
Then you could override enough of ActiveRecord to make it crawl your  
schema instead of making its current assumptions about the  
relationships between the object world and the relational world.
