# Basic relationship question

**URL:** <https://www.ruby-forum.com/t/basic-relationship-question/121695>\
**Category:** Rails\
**Created:** [November 30, 2007, 12:15am UTC](https://www.ruby-forum.com/t/basic-relationship-question/121695 "2007-11-30T00:15:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![silvertab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/22d042/32.png) [@silvertab](https://www.ruby-forum.com/u/silvertab)\
**Post date:** [November 30, 2007, 12:15am UTC](https://www.ruby-forum.com/t/basic-relationship-question/121695/1 "2007-11-30T00:15:57Z")

</div>

This question should be rather easy, but being fairly new to RoR I’m not  
sure of the best way to proceed…

I have 2 models: City and Location… Both model belong to the Country  
model…

(So basically a Country has many cities and many locations)

Now the tricky part is that… a city can also have Locations…

From there, I’m not sure where to go…

## My first idea was to go with the following relationships:

Location:  
belongs\_to :city  
belongs\_to :country

City:  
belongs\_to :country  
has\_many :locations

## Country: has\_many :cities has\_many :location

However, I realized that this meant Locations will have both a city\_id  
and a country\_id field…in some case, the country\_id would be empty, in  
other cases, the city\_id would be empty (depending if the Locations  
belongs to a city or a country)… Would this work? Somehow this doesn’t  
sound right?

I read a bit on polymorphic association… could this be a better idea  
for what I’m trying to do?

Any help would be appreciated!

---

<div class="post-metadata">

**Author:** ![silvertab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/22d042/32.png) [@silvertab](https://www.ruby-forum.com/u/silvertab)\
**Post date:** [November 30, 2007, 10:09am UTC](https://www.ruby-forum.com/t/basic-relationship-question/121695/2 "2007-11-30T10:09:08Z")

</div>

Hi,

I m not quite sure why you do nt describe the relation like that  
Country 1 n City 1 n Location  
which would mean that every Location has a City…  
If you would like to divide between Locations being in a city rather  
than being in a village you could add a type filed to the city  
table…

–  
Volker
