Basic relationship question

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!

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