# RoR : Linking tables without ID column

**URL:** <https://www.ruby-forum.com/t/ror-linking-tables-without-id-column/69908>\
**Category:** Ruby\
**Created:** [September 25, 2006, 2:42pm UTC](https://www.ruby-forum.com/t/ror-linking-tables-without-id-column/69908 "2006-09-25T14:42:12Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philip\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c5a1d2/32.png) [@Philip\_N](https://www.ruby-forum.com/u/Philip_N)\
**Post date:** [September 25, 2006, 2:42pm UTC](https://www.ruby-forum.com/t/ror-linking-tables-without-id-column/69908/1 "2006-09-25T14:42:12Z")

</div>

Folks,

I need to link two tables together which don’t conform to the standard  
Rails  
conventions (it doesn’t have an ID column as the primary key / foreign  
key  
column). Â I’ve got everything working for the COUNTRIES table (primary  
key  
COUNTRY\_CODE), but can’t get the AIRPORTS table LIST function working  
(see  
error at bottom).

Help please.

Phil

Here are the DB definitions (for migration) -

============== START OF DB DEFNS ===============================  
class CreateCountries \< ActiveRecord::Migration  
Â def self.up  
Â Â create\_table(:countries, :id =\> false, :options =\> ’ IN SFPS001’) do  
|t|  
Â Â Â # t.column :name, :string  
Â Â Â t.column :country\_code, :string, :null =\> false, :limit =\> 2  
Â Â Â t.column :country\_name, :string, :null =\> false, :limit =\> 50  
Â Â end  
Â end

Â def self.down  
Â Â drop\_table :countries  
Â end  
end

class CreateAirports \< ActiveRecord::Migration  
Â def self.up  
Â Â create\_table(:airports, :options =\> ‘IN SFPS002’) do |t|  
Â Â Â # t.column :name, :string  
Â Â Â t.column :icao\_code Â , :string, :null =\> false, :limit =\> 4  
Â Â Â t.column :iata\_code Â , :string, :null =\> false, :limit =\> 3  
Â Â Â t.column :town Â Â Â Â , :string, :null =\> false, :limit =\> 100  
Â Â Â t.column :state Â Â Â , :string, Â Â Â Â Â Â Â Â :limit =\> 100  
Â Â Â t.column :airport\_name, :string, Â Â Â Â Â Â Â Â :limit =\> 100  
Â Â Â t.column :country\_code, :string, Â Â Â Â Â Â Â Â :limit =\> 2  
Â Â Â t.column :latitude Â Â , :float Â Â Â  
Â Â Â t.column :longitude Â , :float  
Â Â end

Â Â execute “alter table dbfps001.airports  
Â Â Â Â Â Â Â add constraint r002001a foreign key  
Â Â Â Â Â Â Â (country\_code)  
Â Â Â Â Â Â Â references dbfps001.countries  
Â Â Â Â Â Â Â (country\_code)  
Â Â Â Â Â Â Â on delete restrict”  
Â end

# Â def self.down Â Â drop\_table :airports Â end end ==================== END OF DB DEFNS

Here are the models I’ve defined -

# ==================== START OF MODELS

class Country \< ActiveRecord::Base

Â primary\_key :country\_code  
Â has\_many :trip\_leg\_passengers, :as =\> :passport\_country  
Â has\_many :airports, :as =\> :country\_code  
Â  
Â validates\_presence\_of :country\_code, :country\_name  
Â validates\_uniqueness\_of :country\_code  
Â validates\_uniqueness\_of :country\_name  
end

class Airport \< ActiveRecord::Base

Â has\_many :trip\_legs, :as =\> :start\_airport  
Â has\_many :trip\_legs, :as =\> :end\_airport  
Â belongs\_to :country, :foreign\_key =\> :country\_code  
Â  
Â validates\_presence\_of :icao\_code, :iata\_code, :town  
Â validates\_uniqueness\_of :icao\_code  
Â validates\_uniqueness\_of :iata\_code  
end  
=================== END OF MODELS ================================

And here is the controller for Airports -

================== START OF CONTROLLERS ==========================  
class AirportController \< ApplicationController  
Â #  
Â # Enforce login  
Â #  
Â before\_filter :authorize, :except =\> :login

Â layout “admin”

Â def index  
Â Â list  
Â Â render :action =\> ‘list’  
Â end

Â # GETs should be safe (see  
[URIs, Addressability, and the use of HTTP GET and POST](http://www.w3.org/2001/tag/doc/whenToUseGet.html))  
Â verify :method =\> :post, :only =\> [:destroy, :create, :update],  
Â Â Â Â Â :redirect\_to =\> { :action =\> :list }

Â def list  
Â Â @airports = Airport.find(:all, :include =\> :country)

Â end

Â def show  
Â Â @airport = Airport.find(params[:id])  
Â end

Â def new  
Â Â @airport = Airport.new  
Â end

Â def create  
Â Â @airport = Airport.new(params[:airport])  
Â Â if @airport.save  
Â Â Â flash[:notice] = ‘Airport was successfully created.’  
Â Â Â redirect\_to :action =\> ‘list’  
Â Â else  
Â Â Â render :action =\> ‘new’  
Â Â end  
Â end

Â def edit  
Â Â @airport = Airport.find(params[:id], :include =\> :country)  
Â end

Â def update  
Â Â @airport = Airport.find(params[:id], :include =\> :country)  
Â Â if @airport.update\_attributes(params[:airport])  
Â Â Â flash[:notice] = ‘Airport was successfully updated.’  
Â Â Â redirect\_to :action =\> ‘show’, :id =\> @airport  
Â Â else  
Â Â Â render :action =\> ‘edit’  
Â Â end  
Â end

Â def destroy  
Â Â Airport.find(params[:id]).destroy  
Â Â redirect\_to :action =\> ‘list’  
Â end  
end

================== END OF CONTROLLERS ============================

When I try to list the airport records I get -

============== START OF CUT =============================  
ActiveRecord::StatementInvalid: [IBM][CLI Driver][DB2/LINUX] SQL0206N Â  
“COUNTRIES.ID” is not valid in the context where it is used.  
Â SQLSTATE=42703  
SQLCODE=-206: SELECT airports.id AS t0\_r0, airports.icao\_code AS t0\_r1,  
airports.iata\_code AS t0\_r2, airports.town AS t0\_r3, airports.state AS  
t0\_r4,  
airports.airport\_name AS t0\_r5, airports.country\_code AS t0\_r6,  
airports.latitude AS t0\_r7, airports.longitude AS t0\_r8, countries.id AS  
t1\_r0, countries.country\_code AS t1\_r1, countries.country\_name AS t1\_r2  
FROM  
airports Â LEFT OUTER JOIN countries ON countries.id =  
airports.country\_code  
=============== END OF CUT ==============================

In other words it is trying to join looking for an ID column in  
COUNTRIES.

Here’s the trace -

=============== START OF CUT ============================  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/connection\_adapters/abstract\_adapter.rb:120:in  
`log' /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/ibm_db2_adapter.rb:517:in `execute’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/connection\_adapters/ibm\_db2\_adapter.rb:466:in  
`select_all' /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/associations.rb:1110:in `select\_all\_rows’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/associations.rb:975:in  
`find_with_associations' /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/associations.rb:973:in `find\_with\_associations’  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/base.rb:923:in  
`find_every' /usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:381:in `find’  
app/controllers/airport\_controller.rb:19:in `list' app/controllers/airport_controller.rb:10:in `index’  
=============== END OF CUT ================================
