Backo
Backo

Reputation: 18881

How to organize model classes and related database data for geographical spaces?

I am using Ruby on Rails v3.2.2 and MySQL. I would like to "abstract" and "handle" geographical spaces in a balanced way, but I am in trouble when I have to "invent" the overall structure. So...

How to organize model classes and related database data for geographical spaces? That is, for example, which "type" of classes should I implement - should I use something like RoR Single Table Inheritance or Polymorphic classes? - ? and, given the mentioned model class (or model classes), which table columns should I add to my database and how should those be populated in order to be retrievable (reasons for this are that I am thinking to implement a search form in order to find records in an user friendly way, perhaps with a "autocompletable name" input field)?


Note: At this time, I am planning to "organize" a geographical space in ("nested" spaces) continents > countries > regions > cities > addresses. However, since retrievability for search form use cases, I am wondering to create one only database table in order to handle those space objects.

Upvotes: 0

Views: 86

Answers (1)

Rodrigo Oliveira
Rodrigo Oliveira

Reputation: 913

It think that using belongs_to and has_many would be friendly enough, even for autocompletes and something else.

class Continent < ActiveRecord::Base
    has_many :countries
end 

class Country < ActiveRecord::Base
    belongs_to :continent
    has_many :regions
end

class Region < ActiveRecord::Base
    belongs_to :country
    has_many :cities
end

Something like that.

Upvotes: 1

Related Questions