Created
June 30, 2014 13:36
-
-
Save aravindgd/21ec30146e99b5716cd9 to your computer and use it in GitHub Desktop.
Uploading CSV rails
This file contains 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
namespace :lawyer_city do | |
desc "Import lawyer data" | |
task :all => :environment do | |
import_lawyer_personal_date | |
end | |
def import_lawyer_personal_date | |
records = [] | |
File.open(Rails.root.join("public/lawyer_city.csv") , 'r').each do |line| | |
s = line.split(',') | |
records << s | |
end | |
records.each_with_index do |record, index| | |
puts "+++++++++++++++++++++++++++++++++++++++++++++" | |
puts index | |
puts "+++++++++++++++++++++++++++++++++++++++++++++" | |
lawyer=Lawyer.new(name: record[1],code: record[0], years_of_experience: record[2],location: record[3],rating: record[4]) | |
if lawyer.save! | |
puts "Lawyer data has been saved" | |
end | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment