Showing posts with label strip. Show all posts
Showing posts with label strip. Show all posts

Thursday, July 26, 2012

Import csv file using ruby processor

Hello Guys,
        Let's discuss how to import .csv file into database using ruby processor.

Read the .csv file:

require 'fastercsv'

rows = CSV.read("#{RAILS_ROOT}/test.csv")
header = rows[0]
rows[1..-1].each do |row|
    data = {}
    header.each_with_index do |key, index|
      data[key.downcase] = row[index].strip
    end
    ModelObject.create!(data)
end

test.csv looks like

No,Name,Percentage
1,Priyanka,81
2,Rahul,82
3,Ruby,75