How to download CSV data using ActionController::Live from MongoDB?
Mongoid automatically query your records in batches (More info over here)
To add your records to a CSV file, you should do something like:
records = MyModel.all# By default batch_size is 100, but you can modify it using .batch_size(x)result = CSV.generate do |csv| csv << ["attribute1", "attribute2", ...] records.each do |r| csv << [r.attribute1, r.attribute2, ...] endendsend_data result, filename: 'MyCsv.csv'
Remember that send_data
is an ActionController method!
I think you donĀ“t need SSE for generating a CSV. Just include ActionController::Live
into the controller to use the response.stream.write iterating your collection:
include ActionController::Live...def some_action format.csv do # Needed for streaming to workaround Rack 2.2 bug response.headers['Last-Modified'] = Time.now.httpdate headers['Content-Disposition'] = "attachment; filename=\"products.csv\"" headers['Content-Type'] ||= 'text/csv' [1,2,3,4].each do |i| # --> change it to iterate your DB records response.stream.write ['SOME', 'thing', "interesting #{i}", "#{Time.zone.now}"].to_csv sleep 1 # some fake delay to see chunking end ensure response.stream.close end end
Try it with curl or similar to see the output line by line:
$ curl -i http://localhost:3000/test.csv