Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7949867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:05:52+00:00 2026-06-04T02:05:52+00:00

I need to import a large CSV file, broken down to small chunks that

  • 0

I need to import a large CSV file, broken down to small chunks that will be imported every X hours.

I made the following rake task

task :import_reviews => :environment do
 require 'csv'
 CSV.foreach('reviews.csv', :headers => true) do |row|
  Review.create(row.to_hash)
 end
end

Using heroku scheduler I could let this task run every day, but I want to break it up in several chunks, for example 100 records every day:

That means I need to keep track of the last row imported, and start with that row += 1 the next time I would let the rake task run, how can I implement this?

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T02:05:53+00:00Added an answer on June 4, 2026 at 2:05 am

    Read the rest of the CSV in to an array and outside the CSV.foreach loop write to the same CSV file, so that it gets smaller each time. I suppose i don’t have to give this in code but if necessary comment me and i’ll do.

    If you want to keep the CSV in a whole, add a field “pocessed” to the CSV and fill it with a 1 if read, next time filter these out.

    EDIT: this isn’t tested and sure could be better but just to show what i mean

    require 'csv'
    index = 1
    csv_out = CSV::Writer.generate(File.open('new.csv', 'wb'))
    CSV.foreach('reviews.csv', :headers => true) do |row|
      if index < 101
        Review.create(row.to_hash)
      else
        csv_out << row
      end
      index += 1
    end
    csv_out.close
    

    afterward, dump reviews.csv and rename new.csv to reviews.csv

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to import a large CSV file into an SQL server. I'm using
I've got some large csv files that I need to import into IEnumerable (prob
I need to import a csv file into Firebird and I've spent a couple
I need to import a huge csv data file (6880 columns) and I need
I have this large txt file 8mb+ that is delimited and I need to
We have a rather large library that we need to periodically import (and then
I have many large (>35,000,000) lists of integers that will contain duplicates. I need
I have a large file, with 1.8 million rows of data, that I need
I need to import a large category .txt file to my mysql database. This
I have a CSV file with 550,000+ rows. I need to import this data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.