Skip to content

Instantly share code, notes, and snippets.

@kyleknighted
Created July 12, 2011 18:29
Show Gist options
  • Save kyleknighted/1078620 to your computer and use it in GitHub Desktop.
Save kyleknighted/1078620 to your computer and use it in GitHub Desktop.
class CsvFile < ActiveRecord::Base
belongs_to :user
require "CSV"
has_attached_file :csv_file,
:path => ":rails_root/public/system/csvfile/:id/:filename",
:url => "/system/csvfile/:id/:filename"
after_create :parse_csv
def parse_csv
mapping = Mapping.last
c = CSV.table(self.csv_file.path)
c.entries.each do |row|
vehicle = Vehicle.new
vehicle.year = row[mapping[:year].to_sym]
vehicle.price = row[mapping[:price].to_sym]
vehicle.engine = row[mapping[:engine].to_sym]
vehicle.make = row[mapping[:make].to_sym]
vehicle.color = row[mapping[:color].to_sym]
vehicle.save
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment