0

Ich erstelle eine Tabelle mit dem Namen Routes, die nur Ziel-Airport-ID und Arrival-Airport-ID hat. Ich möchte auf @ route.destination_airport verweisen können, aber Rails erstellt diese Eigenschaften nicht. Hier ist, was ich versucht:Rails: Erstellen Sie zwei Navigationseigenschaften, wenn Sie auf eine Tabelle verweisen

create_table "routes", force: :cascade do |t| 
    t.integer "departure_airport_id" 
    t.integer "arrival_airport_id" 
    end 
    create_table "airports", force: :cascade do |t| 
    t.string "iata_code" 
    t.string "icao_code" 
    t.string "international_name" 
    t.string "localized_name" 
    end 
class Airport < ApplicationRecord 
    scope :sorted, lambda { order("iata_code ASC") } 
    has_many :routes, :foreign_key => :id 
end 
class Route < ApplicationRecord 
    belongs_to :airport, :foreign_key => "arrival_airport_id" 
    belongs_to :airport, :foreign_key => "departure_airport_id" 
end 

Antwort

1

Try this:

class Route < ApplicationRecord 
    belongs_to :arrival_airport, class_name: 'Airport' 
    belongs_to :departure_airport, class_name: 'Airport' 
end 
+0

Bitte meine Antwort aktualisiert sehen. –

Verwandte Themen