Geographic Data, Rails, and Carmen

Instead of using the carmen-rails gem as an interface to carmen’s .yml geographic data, I am trying out a new way: putting carmen’s data directly into my db, and making Country, Subregion, and City into regular ActiveRecord::Base classes, making use of the composite_keys gem.

Also, instead of storing the ISO codes in the database, I am storing the English place-names themselves.

The rake task to move the data from carmen to the db is like this.

And the models are like this.


  • Easier to search database by English country name, because names, rather than codes, are stored in the db.
  • Easier to add user-editable attributes (eg., travel expenses for each subregion) to Country and Subregion models.
  • After importing carmen’s data into your db, eliminates dependencies on carmen and carmen-rails
  • In carmen-rails, it can be tricky to display the place names in the view. Sometimes the value stored in the db is a code to look up a Carmen::Country. But other times, it is a custom place name to be displayed as-is. The logic to figure out how to display each record is too complicated. With this new way, the view code is simplified to @order.country_name


  • Harder to translate and localize
  • Introduces new dependency on the composite_keys gem
  • Doesn’t follow the ‘Rails way’ because database keys are not an ‘id’ integer column
  • Doesn’t follow ISO international standards for country codes

So this alternate way makes sense if:

  • The app is mostly monolingual
  • Users need to search the database for place names, like in a full text search
  • Users need to edit custom Country or Subregion attributes
  • There is no pressing need to use ISO standard codes for places


  1. what about performance?

    1. I have not noticed any difference in performance in my app, whose tables generally have under 10,000 records for the moment.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s