# Legacy tables and foreign keys

**URL:** <https://www.ruby-forum.com/t/legacy-tables-and-foreign-keys/104730>\
**Category:** Rails\
**Created:** [May 21, 2007, 11:11pm UTC](https://www.ruby-forum.com/t/legacy-tables-and-foreign-keys/104730 "2007-05-21T23:11:13Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pat\_Hacker](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@Pat\_Hacker](https://www.ruby-forum.com/u/Pat_Hacker)\
**Post date:** [May 21, 2007, 11:11pm UTC](https://www.ruby-forum.com/t/legacy-tables-and-foreign-keys/104730/1 "2007-05-21T23:11:13Z")

</div>

Not sure what I’m doing wrong. Sorry for the length.  
Setup: ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-darwin8.9.1] /  
Rails 1.2.2

One estimate has many parts.

class Estimate \< OldBase # These are legacy tables that cannot be  
changed.  
set\_table\_name ‘ESTIMATE’  
set\_primary\_key ‘EST\_NUMBER’

has\_many :parts, :class\_name =\> “OldPart”, :foreign\_key =\>  
“EST\_NUMBER”

# Estimate has a field with the name of ‘ID’ used as a human

readable identification – not as a unique id in the sense of rails.  
end  
class OldPart \< OldBase  
set\_table\_name "EST\_PART’  
set\_primary\_key ‘IDNUMBER’ # used as rails would use the field ‘id’

belongs\_to :estimate, :foreign\_key =\> “EST\_NUMBER”

# in OldPart is a field EST\_NUMBER which contains the number of the

parent estimate.  
end

Trying it out

> > reload!  
> > estimate = Estimate.find\_by\_EST\_NUMBER(11355)  
> > pp.estimate.EST\_NUMBER  
> > “11355”
> 
> > pp estimate.ID  
> > "230-0019-00 "
> 
> > pp e.parts  
> > []

Looking in the development log  
SELECT \* FROM ESTIMATE WHERE (ESTIMATE. EST\_NUMBER = 11355)  
SELECT \* FROM EST\_PART WHERE (EST\_PART . EST\_NUMBER = ‘230-0019-00’)

I expected  
SELECT \* FROM ESTIMATE WHERE (ESTIMATE. EST\_NUMBER = 11355)  
SELECT \* FROM EST\_PART WHERE (EST\_PART.EST\_NUMBER = ‘11355’)

Changing  
class OldPart \< OldBase  
…  
belongs\_to :estimate, :foreign\_key =\> “EST\_NUMBER”  
to  
belongs\_to :estimate  
or to  
belongs\_to :estimate, :class\_name =\> “Estimate”, :foreign\_key =\>  
“EST\_NUMBER”  
or just removing the belongs\_to line

has no effect.

Has\_many associations for other tables work fine.

What am I missing? Suggestions?

Thanks  
Pat
