我正在创建一个名为Routes的表,它只有destination_airport_id和arrival_airport_id。我希望能够引用@ route.destination_airport,但Rails不会创建这些属性。这是我试过的:
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
答案 0 :(得分:1)
试试这个:
class Route < ApplicationRecord
belongs_to :arrival_airport, class_name: 'Airport'
belongs_to :departure_airport, class_name: 'Airport'
end