32

Loading Zip Codes with SmarterCSV Gem

 3 years ago
source link: https://fuzzyblog.io/blog/rails/2020/03/18/loading-zip-codes-with-smartercsv-gem.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Loading Zip Codes with SmarterCSV Gem

Mar 18, 2020

IMG_2664.jpeg

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.


Posted In: #rails #csv #covidnearme.org


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK