Legacy tables and foreign keys


#1

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