Skip to content

Instantly share code, notes, and snippets.

@davidpaulhunt
Created September 12, 2014 18:55
Show Gist options
  • Select an option

  • Save davidpaulhunt/e31522ebecafd8b6fc69 to your computer and use it in GitHub Desktop.

Select an option

Save davidpaulhunt/e31522ebecafd8b6fc69 to your computer and use it in GitHub Desktop.
# wines_controller.rb
def import
file = params[:file]
user = current_user.id
UploadCsvJob.new.async.perform(file, user)
redirect_to root_path, notice: "Your wines are being imported! Check your wine list for updates."
end
# wine.rb
def self.import(file, current_user_id)
imported_wines = SmarterCSV.process(file.tempfile, {:force_simple_split => true, :strip_chars_from_headers => /[\-"]/, :remove_unmapped_keys => true, :key_mapping => {:name => :name, :vintage => :vintage, :varietal => :varietal, :price => :price, :notes => :notes, :bottle_size => :bottle_size, :country => :country, :type => :color, :variety => :category}})
imported_wines.each do |row|
wine = Wine.new(row)
wine.user_id = current_user_id
wine.save!
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment