ActiveRecord & Legacy DBs

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs