Home > Ruby, RubyGems, Uncategorized > Import CSV Using Rails Migration

Import CSV Using Rails Migration

Reposted from Rails Weenie:

Install FasterCSV (‘gem install fasterCSV’)

The code to use inside the migration is similar to:

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

Advertisements
Categories: Ruby, RubyGems, Uncategorized
  1. April 21, 2008 at 10:04 pm

    Thank you for this. I needed to do exactly this and your post came in handy.

  2. January 23, 2009 at 3:10 pm

    I ran into the “no such file to load — faster_csv” and “no such file to load — fastercsv” errors when attempting to migrate.

    My solution was to add this to environment.rb:

    config.gem ‘fastercsv’, :lib => ‘faster_csv’

    Then run this from RAILS_ROOT:

    sudo rake gems:install

  3. bala
    September 1, 2009 at 10:41 am

    hi,

    The above code working perfectly in my rails application. I need to change the colour of the row can u help me?

    regards
    k.Balasubramiam

  1. May 15, 2008 at 4:22 pm
  2. September 5, 2008 at 11:03 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: