I’m trying to insert 200k records into three different tables. When I parse the CSV file and try to insert these records, Ruby locks up my entire system. Here is my code:
def upload_country
@upload = Upload.new(:upload => params[:upload])
if @upload.save
csv = CSV.parse(csv_text, :headers => true)
csv.each_with_index do |row, index|
unless row["name"].blank? or row["country_code"].blank? or row["destination"].blank? or row["code"].blank?
@country = Country.new(:name => row["name"].gsub(/\s+/, " ").strip, :country_code => row["country_code"].gsub(/\s+/, " ").strip, :user_id => current_user.id, :subscriber_id => get_subscriber_id)
@country.save
if row["country_code"] == "1"
p = @country.country_code.to_s+@destination.name+row["code"].gsub(/\s+/, " ").strip
else
p = @country.country_code.to_s+row["code"].gsub(/\s+/, " ").strip
end
@code = DestinationCode.create(:code => p, :country_destination_id => 1, :user_id => current_user.id)
end
end
@upload.destroy
@countries = Country.find_all_by_subscriber_id(get_subscriber_id)
render :partial => '/mycarriers/carrier_country', :layout => false
end
why dont use use Mass Insert here a gem that can help you in the quest I personally used it in my application to insert 500K records with resque for the background processing
Hope it help you