Skip to content

Instantly share code, notes, and snippets.

@munya
Created June 28, 2016 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save munya/a8d627da69196a859d95fb7fe6bd7c89 to your computer and use it in GitHub Desktop.
Save munya/a8d627da69196a859d95fb7fe6bd7c89 to your computer and use it in GitHub Desktop.
CSV Data as stream
class PropertiesController < ApplicationController
include ActionController::Live
before_action :find_county
def index
response.headers["Content-Type"] ||= 'text/csv'
response.headers["Content-Disposition"] = "attachment; filename=#{@county.csv_file_name}"
response.stream.write Property::CSV_HEADERS.to_csv
properties = @county.properties.processed
if params[:with_different_addresses_only]
properties = properties.where(has_different_addresses: true)
end
properties.find_in_batches(batch_size: 1000).each do |group|
group.each do |property|
row = []
Property::CSV_COLUMN_NAME.each do |column|
row << property.send(column.to_sym).to_s
end
response.stream.write row.to_csv
end
end
response.stream.close
end
private
def find_county
@county = County.find(params[:county_id])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment