Import CSV Using Rails Migration

Reposted from Rails Weenie:

Install FasterCSV ('gem install fasterCSV')

The code to use inside the migration is similar to:

1
2
3
4
FasterCSV.foreach("#{RAILS_ROOT}/lib/symbols_database/security_list.csv", :row_sep => "r") do |row|
    field1,field2,field3 = row
    Foo.create(:field1 => field1, :field2 => field2, :field3 => field3)
end
  1. Thank you for this. I needed to do exactly this and your post came in handy.

  2. ESPN Dev Blog » Blog Archive » CSV Imports using FasterCSV - pingback on May 15, 2008 at 4:22 pm
  3. Migraciones y CSV con FasterCSV - pingback on September 5, 2008 at 11:03 pm
  4. Me interesó leer esta noticia y los comentarios de los demás. Muy buenos los comentarios

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

Trackbacks and Pingbacks: