On 21 Nov 2007, at 15:43, sabrinih wrote:
>
> I have a reservation class which has origin_city and destination_city,
> both of which are instance of location class. How would I represent
> this using belongs_to relationship. I would like something like:
> class Location ...
> has_one reservation for origin_city
> has_one reservation for destination_city
>
> class Reservaton
> belongs_to :location, :as => origin_city
> belongs_to :location, :as => destination_city
>
That''s not quite right.
On Reservation you want
belongs_to :origin_city, :class_name => ''Location'',
:foreign_key =>
''origin_city_id''
and similarly for the other one
on Location you probably want
has_many :origin_reservations, :class_name =>
''Reservation'', :foreign_key => origin_city_id''
I''m assuming you want has_many instead of has_one unless it''s
the case
that a location can only be used be a single reservation
Fred
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---