Rails multiple associations between two models
Asked Answered
M

2

8

I have Flight, Person, and Glider models in a Rails 3 app. I've defined custom relationships because I need more than one foreign key referencing a Person from the flights table. Associations work but ONE-WAY only.

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'm almost there, but I don't understand how Glider.first.flights does work when Person.first.flights doesn't.

UPDATE: Associations with 'Airfield' works... so I'm clueless as to why it doesn't work with 'Person'

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
Mousy answered 5/4, 2011 at 15:44 Comment(6)
Hi, i created a small rails project with your Flight, Glider and Person class, and for me it just works. I think your data model is a perfect solution for your problem-area. Could you show the exact error? Where does the nomethod error come from? Maybe your class has an after_initialize method?Chapa
Strangely, I just tried again (after three days of banging my head against the wall) and it works now. I added a new record in the flights table and associations works correctly, and they even work with the previous flights I had saved. I'm clueless. Thank you for the heads up, I'll try creating a new rails application when I have a similar problem. How should I answer this question? Perhaps add my own answer with this comment?Mousy
Nice to hear it works for you too now. Not sure what you should do: answer it with that comment or delete it?Chapa
I'll think this question is a useful example of multiple associations between two models, so I'll change the title for something more useful and answer with my commentMousy
Could you please post your database tables? I'm new to Rails and they'd be really helpful. Thanks!Moguel
Here's a gist that might help gist.github.com/1190275Mousy
M
0

I've been told that the association between these models is set correctly.

I added a new record to the flights table, and now the associations work correctly with this new record and all the previous ones. I'm not really sure how it is working now, but it sure does.

Mousy answered 10/4, 2011 at 16:15 Comment(0)
G
0

Do your pilots have types? like a pilot_type column? I just also started reading into these kinds of patterns and luckily it's still a bit fresh(hopefully. please correct me if Im wron rails ninjas! :))

You are in need of the polymorphic pattern as discussed here:

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

Grain answered 5/4, 2011 at 15:50 Comment(3)
Looking for simplicity, I've decided to go with the One Class, One Table model for people. I have an attribute "glider_pilot_status" with the values "none", "trainee" and "pilot"; so when logging flights, the model validates that the "pilot_id" is a person with "glider_pilot_status"="pilot". I'll look into your suggestion but I'm clueless as to why my posted option does not work.Mousy
it would extremely help you to use polymorphism since you're using different pilot types, but good luck to you!Grain
After reading the article, I think there's a better solution rather than polymorphism for the Author. He could have just used Single Table Inheritance because the three types of Article have all attributes in common, and they are mutually exclusive (An article can't be a photo or a Event but not two of those at the same time). In my personal case, one Person can be a Pilot or a Trainee, but after becoming a pilot he can also be a towplane_pilot and an instructor, so mutual exclusivity is not the best fit for me, and they also have different attributes.Mousy
M
0

I've been told that the association between these models is set correctly.

I added a new record to the flights table, and now the associations work correctly with this new record and all the previous ones. I'm not really sure how it is working now, but it sure does.

Mousy answered 10/4, 2011 at 16:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.