Join in legacy DB?

I’ve got a problem with some tables that don’t follow any RAILS
standards. how do I define the join in the model when all three tables
have wacky names?

class Machine < ActiveRecord::Base
set_table_name “tbl_CodeMgmt_Host”
set_primary_key “Id”
end

class Pool < ActiveRecord::Base
set_table_name “tbl_CodeMgmt_Pool”
set_primary_key “Id”
end

the join table is

tbl_CodeMgmt_PoolHost
with columns
CodeMgmtPoolId
CodeMgmtHostId

any clues? thanks

-zaq

I’ve got a problem with some tables that don’t follow any RAILS
standards. how do I define the join in the model when all three tables
have wacky names?>

Something along the lines:

class Machine < ActiveRecord::Base
has_and_belongs_to_many :pools, :join_table =>
‘tbl_CodeMgmt_PoolHost’, :foreign_key => ‘CodeMgmtHostId’,
:assocciation_foreign_key => ‘CodeMgmtPoolId’
set_table_name “tbl_CodeMgmt_Host” # must be below has_… declaration
set_primary_key “Id”

class Pool < ActiveRecord::Base
has_and_belongs_to_many :machines, :join_table =>
‘tbl_CodeMgmt_PoolHost’, :foreign_key =>
‘CodeMgmtPoolIdCodeMgmtHostId’, :assocciation_foreign_key =>
‘CodeMgmtHostId’
set_table_name “tbl_CodeMgmt_Pool” # must be below has_… declaration
set_primary_key “Id”
end

Even if I have messed things up, :foreign_key,
:association_foreign_key and :join_table_name are the things to look
at.

Regards,
Rimantas

http://rimantas.com/

class Pool < ActiveRecord::Base
has_and_belongs_to_many :machines, :join_table =>
‘tbl_CodeMgmt_PoolHost’, :foreign_key =>
‘CodeMgmtPoolIdCodeMgmtHostId’, :assocciation_foreign_key =>
‘CodeMgmtHostId’

Oops…

has_and_belongs_to_many :machines,
:join_table => ‘tbl_CodeMgmt_PoolHost’,
:foreign_key => ‘CodeMgmtPoolId’, :assocciation_foreign_key =>
‘CodeMgmtHostId’

Regards,
Rimantas

http://rimantas.com/

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