# Two belong\_to to same model

**URL:** <https://www.ruby-forum.com/t/two-belong-to-to-same-model/120901>\
**Category:** Rails\
**Created:** [November 21, 2007, 4:44pm UTC](https://www.ruby-forum.com/t/two-belong-to-to-same-model/120901 "2007-11-21T16:44:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sabrinih](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dfb087/32.png) [@sabrinih](https://www.ruby-forum.com/u/sabrinih)\
**Post date:** [November 21, 2007, 4:44pm UTC](https://www.ruby-forum.com/t/two-belong-to-to-same-model/120901/1 "2007-11-21T16:44:32Z")

</div>

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

Can someone correct the exact syntax for me.  
Thanks.

---

<div class="post-metadata">

**Author:** ![sabrinih](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dfb087/32.png) [@sabrinih](https://www.ruby-forum.com/u/sabrinih)\
**Post date:** [November 21, 2007, 4:50pm UTC](https://www.ruby-forum.com/t/two-belong-to-to-same-model/120901/2 "2007-11-21T16:50:59Z")

</div>

On 21 Nov 2007, at 15:43, sabrinih wrote:

> 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
