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