Skip to content

Instantly share code, notes, and snippets.

@sandeshbodake
Created August 5, 2020 17:40
Show Gist options
  • Save sandeshbodake/84559615794b5cc772540a98dbd4088c to your computer and use it in GitHub Desktop.
Save sandeshbodake/84559615794b5cc772540a98dbd4088c to your computer and use it in GitHub Desktop.
def self.export_data_to_csv
headers = ["Zipcode", "Locality Type", "Locality Name", "State", "State Abbr", "Country"]
Locality.all.grou_by(&:state_abbr).each do |state, records|
file = File.new("public/#{state}.csv", "w")
CSV.open(file, 'w', write_headers: true, headers: headers) do |writer|
records.each do |locality|
writer << [locality.zipcode, locality.locality_type, locality.name, locality.state, locality.state_abbr, locality.country]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment