0

나는 routes라고 불리는 테이블을 만들고 있는데, destination_airport_id와 arrival_airport_id 만 있습니다. @ route.destination_airport를 참조 할 수 있기를 원하지만 레일스는 이러한 속성을 생성하지 않습니다. 여기에 내가 시도 내용은 다음과 같습니다레일 : 하나의 테이블을 참조 할 때 두 개의 탐색 속성을 만듭니다.

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 

답변

1

이 시도 :

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

내 업데이트 된 대답을 참조하십시오. –