# Dealing with not-so-nice legacy schema

**URL:** <https://www.ruby-forum.com/t/dealing-with-not-so-nice-legacy-schema/159559>\
**Category:** Rails\
**Created:** [February 5, 2009, 8:02pm UTC](https://www.ruby-forum.com/t/dealing-with-not-so-nice-legacy-schema/159559 "2009-02-05T20:02:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ojis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/bc79bd/32.png) [@Ojis](https://www.ruby-forum.com/u/Ojis)\
**Post date:** [February 5, 2009, 8:02pm UTC](https://www.ruby-forum.com/t/dealing-with-not-so-nice-legacy-schema/159559/1 "2009-02-05T20:02:41Z")

</div>

Hello Gurus!

I have a problem with legacy database-schema and I would like to get  
some ideas and suggestions how to handle it with ActiveRecord. First  
the idea with database is that there can be several versions of the  
same data. Current data is only one row but the same data can be saved  
on several rows.

Example:  
identification varchar2(10)  
area varchar2(30)  
owner varchar2(50)  
effective\_date datetime  
termination\_date datetime

The SQL used to get current data is:  
select \* from example\_table e where e.identification=‘123456’ and  
termination\_date is null and effective\_date=(select max  
(effective\_data) from example\_data where identification=‘123456’)

What I would like to achieve is at least partially RESTful wrapper  
around the datamodel. There are dozens of tables like the example and  
there aren’t any integrity constraints defined (no foreign keys used).  
Responsibility of the data-integrity is pushed to the application  
logic.

Yes, if it were up to me the legacy schema would be modified and made  
more sensible. Unfortunately I cannot affect since my application  
isn’t the only one using it. So I’m stuck with situation where I just  
have to live with it.

I probably build couple of prototypes for managers with Sinatra before  
making a full-blown rails-app. What would be the best way to wrap this  
kind of datamodel under ActiveRecord-umbrella? Loads of custom SQL at  
least I think.

---

<div class="post-metadata">

**Author:** ![Ojis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/bc79bd/32.png) [@Ojis](https://www.ruby-forum.com/u/Ojis)\
**Post date:** [February 6, 2009, 4:16am UTC](https://www.ruby-forum.com/t/dealing-with-not-so-nice-legacy-schema/159559/2 "2009-02-06T04:16:57Z")

</div>

Sounds fine to me. What’s the problem?

Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Learn rails: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)
