# Has/Belongs Relationships for Address Book

**URL:** <https://www.ruby-forum.com/t/has-belongs-relationships-for-address-book/149663>\
**Category:** Rails\
**Created:** [September 27, 2008, 6:18pm UTC](https://www.ruby-forum.com/t/has-belongs-relationships-for-address-book/149663 "2008-09-27T18:18:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jason](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jason](https://www.ruby-forum.com/u/jason)\
**Post date:** [September 27, 2008, 6:18pm UTC](https://www.ruby-forum.com/t/has-belongs-relationships-for-address-book/149663/1 "2008-09-27T18:18:43Z")

</div>

I am new to RoR, so I will apologize in advance. However, I am  
creating an mapping system with the Google API. So I have a model  
called Routes and a model called Locations. These are related in the  
fact that Routes have a start and end Location. Yet, I can’t determine  
the relationship to use. So…

Idea 1: HABTM relationship, and adding a column to the join table for  
start/end. But that “feels wrong”.  
Idea 2: Add options on a has\_one relationship to map it accordingly.  
Idea 3: Something I haven’t thought of…

I don’t know. Again, I’m new. Any direction would be appreciated.

Thanks,  
Jason

---

<div class="post-metadata">

**Author:** ![jason](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jason](https://www.ruby-forum.com/u/jason)\
**Post date:** [September 27, 2008, 7:34pm UTC](https://www.ruby-forum.com/t/has-belongs-relationships-for-address-book/149663/2 "2008-09-27T19:34:20Z")

</div>

Sent from my iPhone

On 27 Sep 2008, at 17:18, Jason [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am new to RoR, so I will apologize in advance. However, I am  
> creating an mapping system with the Google API. So I have a model  
> called Routes and a model called Locations. These are related in the  
> fact that Routes have a start and end Location. Yet, I can’t determine  
> the relationship to use. So…

Sounds to me like a route would have two belongs\_to, one for the start  
and one for the end. Another way (which may or may not be relevant)  
would be for routes to has\_many locations through waypoints: the join  
table would have a column specifying the order and would define the  
locations through which the route passes (of which start and end are  
just special cases)

Fred

---

<div class="post-metadata">

**Author:** ![jason](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jason](https://www.ruby-forum.com/u/jason)\
**Post date:** [September 30, 2008, 12:16am UTC](https://www.ruby-forum.com/t/has-belongs-relationships-for-address-book/149663/3 "2008-09-30T00:16:17Z")

</div>

I understand what you are suggesting. However, which way is “better”  
practice? Should I use the two relationships, or one with a join table  
that contains the extra column.

Can anyone provide a more specific solution…

---

<div class="post-metadata">

**Author:** ![jason](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jason](https://www.ruby-forum.com/u/jason)\
**Post date:** [October 1, 2008, 12:38am UTC](https://www.ruby-forum.com/t/has-belongs-relationships-for-address-book/149663/4 "2008-10-01T00:38:47Z")

</div>

Cool. Again, this is what I figured. If I go the two relationships in  
routes, what type of relationship is this… belongs\_to?

So I would write something like the following:

class Route  
belongs\_to :start, :class\_name =\> ‘Location’, :foreign\_key =\>  
“start\_location”  
belongs\_to :end, :class\_name =\> ‘Location’, :foreign\_key =\>  
“end\_location”  
…

??

On Sep 29, 8:32 pm, “Rodrigo F.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![jason](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@jason](https://www.ruby-forum.com/u/jason)\
**Post date:** [September 30, 2008, 2:32am UTC](https://www.ruby-forum.com/t/has-belongs-relationships-for-address-book/149663/5 "2008-09-30T02:32:37Z")

</div>

2008/9/29 Jason [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I understand what you are suggesting. However, which way is “better”  
> practice? Should I use the two relationships, or one with a join table  
> that contains the extra column.

I am new to rails, but very old in databases:

If a route has two and will never have more than two nor less than  
two, you have to create two relationships, one called begin and other  
called end. Else, you have to create a table HABTM, where one route  
describes the full stack of relationships (with some kind of  
sorted-by).

Regards,

–  
Rodrigo F.  
[http://www.thecodekeeper.net/](http://www.thecodekeeper.net/)
