Last active
December 14, 2015 22:28
-
-
Save richardjortega/5158205 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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