I made myself a list of countries in YAML format for use in a Rails app today.

Here it is: countries.yml

You can put this file into a directory called db/seed_data and then set up a rake task to load it into your database. Put this into the file seed_data.rake in the lib/tasks directory:

namespace :seed_data do
  desc 'Load seed data into the database of the current environment'
  task :load => :environment do
    require 'active_record/fixtures'
    Dir.glob(RAILS_ROOT + '/db/seed_data/*.yml').each do |file|
      Fixtures.create_fixtures('db/seed_data', File.basename(file, '.*'))
    end
  end
end

You can then issue the following command to load the seed data into the database. It will load into the development database by default — use the RAILS_ENV=production parameter to get it into the production database.

rake seed_data:load

Before you do that, however, you are going to need a Country model to access your countries through.

(more…)