# ActiveRecord & Legacy DBs

**URL:** <https://www.ruby-forum.com/t/activerecord-legacy-dbs/72412>\
**Category:** Ruby\
**Created:** [October 17, 2006, 3:10am UTC](https://www.ruby-forum.com/t/activerecord-legacy-dbs/72412 "2006-10-17T03:10:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/df705f/32.png) [@ike](https://www.ruby-forum.com/u/ike)\
**Post date:** [October 17, 2006, 3:10am UTC](https://www.ruby-forum.com/t/activerecord-legacy-dbs/72412/1 "2006-10-17T03:10:46Z")

</div>

I have a legacy MySQL DB which is ALMOST Rails-ready, i.e. the table  
names  
are correct, they all have a unique integer index ‘id’ etc. However, I  
have  
a table, ‘leads’ which contains an index to ‘origins’ such that  
leads.howdidyouhear=origins.id. If this were truly Rails-ready, the  
leads.howdidyouhear would be leads.origins\_id, but, I cannot rename it.  
The  
db’s look like this:

CREATE TABLE `origins` (  
`id` int(11) NOT NULL auto\_increment,  
`originname` varchar(60) NOT NULL default ‘’  
)

CREATE TABLE `leads` (  
`id` int(11) NOT NULL auto\_increment,  
`howdidyouhear` int(11) NOT NULL default ‘0’,  
`amount` double NOT NULL default ‘0’,  
`homeplan` int(3) NOT NULL default ‘0’  
)

My problem is mapping this in active record. When I map it as follows (I  
am  
not certain this is correct, can someone please confirm?) :

class Origin \< ActiveRecord::Base  
has\_many :leads, :foreign\_key =\> ‘originname’  
end

class Lead \< ActiveRecord::Base  
belongs\_to :origin, :foreign\_key =\> ‘howdidyouhear’  
end

However, when I create:

class LeadController \< ApplicationController  
scaffold :lead  
end

The scaffolding for /lead still shows the integer for `howdidyouhear`.  
Shouldn’t it show the originname String for the howdidyouhear int when  
it  
displays?

Thanks, Ike

---

<div class="post-metadata">

**Author:** ![ike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/df705f/32.png) [@ike](https://www.ruby-forum.com/u/ike)\
**Post date:** [October 17, 2006, 9:28am UTC](https://www.ruby-forum.com/t/activerecord-legacy-dbs/72412/2 "2006-10-17T09:28:46Z")

</div>

> The scaffolding for /lead still shows the integer for `howdidyouhear`.  
> Shouldn’t it show the originname String for the howdidyouhear int when  
> it  
> displays?

First, test that the models work in the console or unit tests.  
Second, I’m not sure if scaffolding normalises an association to show a  
string field of the associated object instead of just the id. It  
wouldn’t know which text field to show, I don’t think. You may need to  
generate the scaffolding, and modify the Rhtml files to show the correct  
value.

Nic
