Seeding Rails App With RESTful Address Data

Grabbing data from Restful webservices has never been easier. I use this code in the Rails /db/seeds.rb file to grab data from the Danish webservice with addresses, municipalities, regions, etc.

I decided to import the data into a local database to ensure it could work offline.

Code from /db/seeds.rb file:

Now I can access municipalities in a grouped selectbox like so in the view code:

 options = option_groups_from_collection_for_select(Region.all, :kommuner, :name, :id, :name) 

<%="kommune_id", options, {}, {:class => "short"}) 

The final result rendered in the user interface:

This code is based on Rails 2.3.5.

One Response to “Seeding Rails App With RESTful Address Data”

  1. Neeraj Dhiman Says:

    Great stuff and thanks for doing all of this.

    Quick question…given that I’m already using Factories for my testing, what is the advantage of using seed_fu over a custom rake task using Factories? It seems like I’d be just re-creating my Factory code.

    Note that my comments relate to creating data for development and testing purposes – not seeding a database with static data (e.g. AK, AZ….).

    thanks again, great stuff