Rails unites several associations between two models

I have Flight, Person, and Glider models in a Rails 3 application. I defined a user relationship because I need more than one foreign key referring to a Person from the flight table. Associations only work with ONE.

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 


 ####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 

I am almost there, but I do not understand how Glider.first.flights works when Person.first.flights does not work.

UPDATE: Associations with Airfield work ... so I don’t know why this does not work with Face

 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 
+8
ruby-on-rails activerecord ruby-on-rails-3 foreign-key-relationship
source share
2 answers

I was told that the connection between these models was established correctly.

I added a new entry to the flight table, and now associations are working correctly with this new entry and all previous ones. I'm not sure how it works now, but it is sure.

0
source share

Do your pilots have types? how is the pilot_type column? I just started reading these templates and, fortunately, still a little fresh (I hope, please correct me if Im wron rails ninjas! :))

You need a polymorphic pattern, as described here:

http://asciicasts.com/episodes/154-polymorphic-association

0
source share

All Articles