Skip to content

Instantly share code, notes, and snippets.

@richardjortega
Last active December 14, 2015 22:28
Show Gist options
  • Save richardjortega/5158205 to your computer and use it in GitHub Desktop.
Save richardjortega/5158205 to your computer and use it in GitHub Desktop.
require_relative "../marketinglist/upload_csv_to_db.rb"
## You would run this with: $ rake myrakes:upload_blast_list
namespace :myrakes do
desc "Uploads a given CSV file to ActiveRecord table: Blast"
task :upload_blast_list => :environment do
#Uncomment blast to delete all prior data
puts "Dropping data from Blasts table"
Blast.delete_all
filename = "#{Rails.root}/directory_you_stored_file/upload_list.csv"
run = UploadMarketingList.new filename
run.main
end
end
require 'csv'
require 'rubygems'
require_relative "../../app/models/blast"
class UploadMarketingList
def initialize(filename)
@filename = filename
end
def main
count = 1
CSV.foreach(@filename, :encoding => 'windows-1251:utf-8') do |row|
record = Blast.new(
:name => row[0],
:url => row[1]
)
puts "Saved #{count} : #{record.name} into the Blast table"
record.save!
count += 1
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment