Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Exporting to CSV without constructing CSV in memory

Rails 2.3.5, Ruby 1.8.7.

In a prior question, there are a number of solutions to exporting data in CSV format, but some of them seem to construct the data in memory before sending. Is this wise when exporting large data sets? Do any of the solutions in the prior question avoid this.

Or is it impossible to avoid building a response without building the whole response locally, either in memory or in a temp file?

I would not be surprised if the latter was true, since if there is an error in the CSV generation, you might want to send an error message back instead, but I might be generating way too much data to want to generate the data in memory/on disk.

like image 220
Thomas Andrews Avatar asked Apr 22 '26 22:04

Thomas Andrews


1 Answers

You can stream directly from the CSV library (FasterCSV in Ruby 1.8).

render :text => proc { |response, output|
    CSV.generate(output) do |csv|
      csv << ...
    end
}

You should also use find_in_batches if you are concerned about your memory footprint.

like image 147
Austin Taylor Avatar answered Apr 24 '26 10:04

Austin Taylor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!