http://www.stephenbartholomew.co.uk/2007/7/20/csv-importer-for-rails
Importer imports data from a CSV and creates record in the application database.
Organisation.import(:from => '/path/to/file.csv')
This will extract data straight from the CSV
Importer will also take a raw StringIO from a form POST:
Organisation.import(:from => params[:organisations])
If no columns are specified, Importer will use the first row as column names. To set columns, you can use the columns option:
Organisation.import(:from => '/path/to/file.csv', :columns => ['name','email'])
If you have a CSV with column names in the first row that do not correspond with your database columns, you can map them with a hash:
Organisation.import(:from => '/path/to/file.csv', :map => {'Org' => 'name', 'EMail' => 'email'})
This is particularly useful if your CSV is generated automatically from another system that you have no control over.
Take the row following for example:
"Curve21","South-East England","www.curve21.com","info@curve21.com"
‘South-East England’ is this organisation’s region. If Organisation has_many :regions then we need a way to look up the region in our database using the name specified in this row. Importer achieves this using Proc objects:
Organisation.import(:from => '/path/to/file.csv', :map => {'region' => 'region_id'}, :associations => {'region_id' => lambda {|a| Region.find_by_name(a) } }
Currently, this is only designed to support only belongs_to and has_one relationships.
Error handling uses logger at the moment and warns when an associated record was not found