# Loading Zip Codes with SmarterCSV Gem

DevFeed: [Loading Zip Codes with SmarterCSV Gem](<https://devfeed.tech/articles/loading-zip-codes-with-smartercsv-gem-28266.md>)

Original publisher: [Read original article](<http://fuzzyblog.io/blog/rails/2020/03/18/loading-zip-codes-with-smartercsv-gem.html>)

Author: Fuzzygroup

Published: 2020-03-18T00:00:00Z

Content type: tutorial

Language: en

Sources: [Scott Johnson](<https://devfeed.tech/sources/scott-johnson.md>)

Topics: [Rails](<https://devfeed.tech/topics/rails.md>), [CSV](<https://devfeed.tech/topics/csv.md>), [Code](<https://devfeed.tech/topics/code.md>), [import](<https://devfeed.tech/topics/import.md>), [data](<https://devfeed.tech/topics/data.md>)

Tags: [code](<https://devfeed.tech/tags/code.md>), [covidnearme-org](<https://devfeed.tech/tags/covidnearme-org.md>), [csv](<https://devfeed.tech/tags/csv.md>), [data](<https://devfeed.tech/tags/data.md>), [import](<https://devfeed.tech/tags/import.md>), [rails](<https://devfeed.tech/tags/rails.md>), [rake](<https://devfeed.tech/tags/rake.md>), [trace](<https://devfeed.tech/tags/trace.md>), [transformation](<https://devfeed.tech/tags/transformation.md>), [zip](<https://devfeed.tech/tags/zip.md>)

## AI overview

A Rails example shows how to load U.S. ZIP code data with the SmarterCSV gem while preserving leading zeros. It uses a rake task and excludes the ZIP field from numeric conversion because the documented transformation did not work as expected.

## Source excerpt

Zip code data is always problematic due to the leading zeros. The smarter_csv gem in Rails defaults to automatically handling numbers as numerics, not strings, which means that you lose 00408 to just 408. Here's an example of a work around using a rake task to load data: # bundle exec rake data:import_geography --trace task :import_geography => :environment do path = File.join(Rails.root, 'lib/tasks/data/', 'Geocodes_USA_with_Counties.csv') geographies = SmarterCSV.process(path, { hash_transformations: [ :convert_values_to_numeric_unless_leading_zeros], convert_values_to_numeric: { except: [:zip] }} ) geographies.each do |g| g_obj = Geography.find_or_create(g) end end Note: This wasn't the accepted work around but a hack; sigh. The :convert_values_to_numeric_unless_leading_zeros transformation doesn't seem work. Issue filed; I hope to fix this myself down the road.