Philip Nelson
2006-Nov-12 22:35 UTC
Two Foreign Keys between Same Tables : How To Display Data
My model is - class TripLeg < ActiveRecord::Base belongs_to :airport, :foreign_key => :start_airport_id belongs_to :airport, :foreign_key => :end_airport_id belongs_to :trip has_many :trip_leg_passengers validates_presence_of :trip_id, :trip_id_sequence, :start_airport_id, :end_airport_id end You''ll notice the two foreign keys to airport, for the start and end point of the trip leg. I now want to display the airport names on a view. I''d normally code - trip_leg.airport.airport_city but this doesn''t work here. How do I tell it to go from a specific column to airport ? Thanks Phil --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Philip Nelson
2006-Nov-12 22:40 UTC
Re: Two Foreign Keys between Same Tables : How To Display Data
Worked it out with help from IRC Changed to - belongs_to :start_airport, :class_name => ''Airport'', :foreign_key => :start_airport_id belongs_to :end_airport , :class_name => ''Airport'', :foreign_key => :end_airport_id and then I code - trip_leg.start_airport.airport_city trip_leg.end_airport.airport_city Phil On Sunday 12 November 2006 22:35, Philip Nelson wrote:> My model is - > > class TripLeg < ActiveRecord::Base > belongs_to :airport, :foreign_key => :start_airport_id > belongs_to :airport, :foreign_key => :end_airport_id > belongs_to :trip > has_many :trip_leg_passengers > validates_presence_of :trip_id, :trip_id_sequence, :start_airport_id, > :end_airport_id end > > You''ll notice the two foreign keys to airport, for the start and end point > of the trip leg. > > I now want to display the airport names on a view. I''d normally code - > > trip_leg.airport.airport_city > > but this doesn''t work here. > > How do I tell it to go from a specific column to airport ? > > Thanks > > Phil > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---