# Migration of existant data

**URL:** <https://www.ruby-forum.com/t/migration-of-existant-data/169744>\
**Category:** Rails\
**Created:** [June 8, 2009, 9:09am UTC](https://www.ruby-forum.com/t/migration-of-existant-data/169744 "2009-06-08T09:09:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![moustach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6f9a4e/32.png) [@moustach](https://www.ruby-forum.com/u/moustach)\
**Post date:** [June 8, 2009, 9:09am UTC](https://www.ruby-forum.com/t/migration-of-existant-data/169744/1 "2009-06-08T09:09:34Z")

</div>

Hi,

I’m actually coding an application which use data from an other one.  
(Zone \> Region \> Country \> Location exactly) which are not in the best  
form. There are primary keys defined on the names of the zone / region /  
… and link between tables with foreign keys using the primary keys of  
the parrent table (logical)

I must use this data but I must use the Rails format in the database  
(foreign keys on id)  
I can’t modify the other database / application.

Do you have an idea how to realize that ?

PS: sorry for my poor english :s

---

<div class="post-metadata">

**Author:** ![moustach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6f9a4e/32.png) [@moustach](https://www.ruby-forum.com/u/moustach)\
**Post date:** [June 8, 2009, 10:33am UTC](https://www.ruby-forum.com/t/migration-of-existant-data/169744/2 "2009-06-08T10:33:18Z")

</div>

It is not clear to me whether you need to access the data in the  
existing database from within a rails app, keeping the existing db  
format, or whether you are trying to do a one time extraction of the  
data from the existing database into a new one which is in  
conventional rails format. ( You said you cannot change the existing  
db, but you also said you must use the rails format in the db).

Colin

2009/6/8 Arnaud G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![moustach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6f9a4e/32.png) [@moustach](https://www.ruby-forum.com/u/moustach)\
**Post date:** [June 8, 2009, 12:12pm UTC](https://www.ruby-forum.com/t/migration-of-existant-data/169744/3 "2009-06-08T12:12:36Z")

</div>

In a short time after the application goes on, there will be an  
automatic update of the location tables from the other database. Because  
the structure of each database (original one and mine) are not the same,  
it will be harder. Is there a trick to handle this problem in Rails (to  
keep the classic structure for primary and foreign keys in Rails) or is  
it better to keep the original struture i would like to import for  
primary and foreign keys ? (and use a plugin to define primary / foreign  
keys in Rails)

---

<div class="post-metadata">

**Author:** ![moustach](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/6f9a4e/32.png) [@moustach](https://www.ruby-forum.com/u/moustach)\
**Post date:** [June 9, 2009, 11:17am UTC](https://www.ruby-forum.com/t/migration-of-existant-data/169744/4 "2009-06-09T11:17:57Z")

</div>

Here is a part of the struture :

==== Migration - create zones  
…  
t.string :code\_zone, :limit =\> 5  
t.string :desc\_zone, :limit =\> 40  
t.string :tool\_status, :limit =\> 1  
…

==== Model zone  
validates\_uniqueness\_of :code\_zone  
has\_many :regions

==== Migration - add zones  
…  
Zone.create(:code\_zone =\> ‘EUROP’, :desc\_zone =\> ‘Europe’, :tool\_status  
=\> ‘A’)  
Zone.create(:code\_zone =\> ‘NAMER’, :desc\_zone =\> ‘North America’,  
:tool\_status =\> ‘A’)  
…

==== Migration - create regions  
…  
t.string :code\_region, :limit =\> 5  
t.string :desc\_region, :limit =\> 40  
t.string :tool\_status, :limit =\> 1  
t.references :code\_zone  
…

==== Model region  
validates\_uniqueness\_of :code\_region  
belongs\_to :zone

==== Migration - add regions  
…  
Region.create(:code\_region =\> ‘AFRIC’, :desc\_region =\> ‘Africa’,  
:tool\_status =\> ‘A’, :code\_zone =\> ‘INTER’)  
Region.create(:code\_region =\> ‘ASIA’, :desc\_region =\> ‘Asia Pacific’,  
:tool\_status =\> ‘A’, :code\_zone =\> ‘ASIA’)  
Region.create(:code\_region =\> ‘USA’, :desc\_region =\> ‘USA’, :tool\_status  
=\> ‘A’, :code\_zone\_id =\> ‘NAMER’)  
…

I would like to add a find method which return the id of the code zone  
name entered within Region.create(…)

Is it possible to rewrite the create method used to create entries in  
the database ?
