레일즈 3 앱에서 Flight, Person 및 Glider 모델을 보유하고 있습니다. flight 테이블의 Person을 참조하는 둘 이상의 외래 키가 필요하기 때문에 사용자 정의 관계를 정의했습니다. 협회는 일하지만 오직 한 가지. 두 모델 간의 다중 연결을 레일 링합니다.
####What works#####
Flight.first.glider
Flight.first.rep_glider
Flight.first.pilot
Flight.first.instructor
Flight.first.towplane_pilot
Flight.first.airplane_instructor
Glider.first.flights
Glider.first.replaced_flights
####What doesn't work#### ----> NoMEthodError 'match'
Person.first.flights
Person.first.instructed_flights
Person.first.towed_flights.
Person.first.instructed_towing_flights
내가 거의 다 해요,하지만 Person.first.flights
하지 않는 경우 내가 Glider.first.flights
이 작업을 수행하는 방법을 이해하지 않습니다
class Flight < ActiveRecord::Base
belongs_to :pilot, :class_name => "Person"
belongs_to :instructor, :class_name => "Person"
belongs_to :towplane_pilot, :class_name => "Person"
belongs_to :airplane_instructor, :class_name => "Person"
belongs_to :glider
belongs_to :rep_glider, :class_name => "Glider"
belongs_to :departure_airfield, :class_name => "Airfield"
belongs_to :arrival_airfield, :class_name => "Airfield"
end
class Glider < Aircraft
has_many :flights
has_many :replaced_flights, :foreign_key => "rep_glider_id", :class_name => "Flight"
end
class Person < ActiveRecord::Base
has_many :flights, :foreign_key => "pilot_id", :class_name => "Flight"
has_many :instructed_flights, :foreign_key => "instructor_id", :class_name => "Flight"
has_many :towed_flights, :foreign_key => "towplane_pilot_id", :class_name => "Flight"
has_many :instructed_towing_flights, :foreign_key => "airplane_instructor_id", :class_name => "Flight"
end
.
UPDATE : '비행장'와 협회 그래서 그것은 '사람'
class Airfield < ActiveRecord::Base
has_many :takeoff_flights, :foreign_key => "departure_airfield_id", :class_name => "Flight"
has_many :grounded_flights, :foreign_key => "arrival_airfield_id", :class_name => "Flight"
end
###Works Correctly
Airfield.first.takeoff_flights
Airfield.first.grounded_flights
Flight.first.departure_airfield
Flight.first.arrival_airfield
안녕하세요, 저는'Flight','Glider' 및'Person' 클래스로 작은 레일 프로젝트를 만들었고 제게 그것은 작동합니다. 데이터 모델이 문제 영역에 대한 완벽한 솔루션이라고 생각합니다. 정확한 오류를 표시 할 수 있습니까? 어디에서 노름 오류가 발생 했습니까? 어쩌면 당신의 클래스는'after_initialize' 메소드를 가지고 있을까요? – nathanvda
이상하게도, 나는 (벽에 머리를 두드리는 삼일 후에) 다시 시도해 보았다. 나는 비행기 표에 새로운 기록을 추가했고 협회는 올바르게 작동하며, 내가 구해 놓았던 이전 비행편과도 작업합니다. 나는 우둔 해. 머리를 가져 주셔서 감사합니다. 비슷한 문제가있을 때 새로운 레일 어플리케이션을 만들려고 노력할 것입니다. 이 질문에 어떻게 대답해야합니까? 아마도이 설명과 함께 내 자신의 대답을 추가 할 수 있습니까? – bruno077
듣기 좋게 듣고 나도 당신을 위해 일합니다. 어떤 조치를 취해야할지 잘 모르겠다 : 그 의견으로 대답하거나 삭제 하시겠습니까? – nathanvda