Store countries with ISO-3166 codes in rails

I’ve been trying to give rebirth to an old internal project at my company, abandoned circa 2006, and this is one of the code pieces I think it could be useful to others.

I wanted to store country information as an ISO code, so here is the plugin to make it work

It’s translated using the translation from the iso-codes package, so I guess it’s as good as it can get by now. If you want to contribute, please do it to the iso-codes package and let me know, so I can update it too.

iso_countries – Store countries using ISO 3166 codes

This rails plugin enables you to store country info only with the country’s ISO-3166 code

Example

  class Company < ActiveRecord::Base
    iso_country :country
  end

  c = Company.new :country => "es"
  c.country                 # => "es"
  c.country_name            # => "Spain"
  c.country_name = "France"
  c.country                 # => "fr"
  ISO::Countries.set_language "es"
  c.country_name            # => "Francia"

Download

You can get it from http://github.com/koke/iso_countries/tree/master