Linking tables with non-ID primary key / foreign key pair


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.


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

  def self.down
    drop_table :countries

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

    execute "alter table dbfps001.airports
             add constraint r002001a foreign key
             references dbfps001.countries
             on delete restrict"

  def self.down
    drop_table :airports
==================== 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

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 OF MODELS ================================

And here is the controller for Airports -

================== START OF CONTROLLERS ==========================
class AirportController < ApplicationController