Hoopla!

now with extra whiz-bang!

Hoopla!

How to integrate zipcode data into your Rails App

June 01, 2006 · 0 comments

Earlier I posted a list of all US zipcodes by city and state. I needed it for one of my sites and now I’m finding it handy for another. Instead of having people fill City, State, and Zip individually (with a high probability that the data won’t be standardized enough to do comparisons between records), you can simply accept a zip code from them and populate the rest yourself.

I create a Zip model that has the following three fields: `code, city, state`. I import the data into it like so (dependant on mysql):

class CreateZips < ActiveRecord::Migration
  def self.up
    create_table :zips do |t|
      t.column  :city,      :string
      t.column  :state,     :string
    end
    # load the zipcodes from a csv file
    execute "load data infile '#{RAILS_ROOT}/db/migrate/zips.csv' into table zips fields terminated by ',' lines terminated by '\n'" 
  end

  def self.down
    drop_table :zips
  end
end
And I make sure my zips.csv file can be found in the ./db/migrate folder.

To associate this model with others, just add a `belongs_to :zip` and make sure that you’ve got a `zip_id` in the table schema. If you use the above migration the zipcode will be set as the table id and you can manually set zip_id to whatever zip code you want – handy for writing fixtures!

Now in my forms I only need to have a 5-char field that asks for a zipcode and (using lovely, lovely AJAX) displays the corresponding city and state automatically.

→ 0 comments Tags:

Zipcodes by City and State - Complete Listing

May 28, 2006 · 2 comments

It took me forever to find this list. I figured I’d post it prominently just so nobody else had to go through the same trouble.

This is a CSV file of zip codes, main city within the zip code (by the USPS’s reckoning) and the two-letter state abbreviation.

Download directly: download zipcode list as csv file

Or view this full post to see a short sample from the complete list.
ZIP,City,ST
00501,Holtsville,NY
00544,Holtsville,NY
00601,Adjuntas,PR
00602,Aguada,PR
00603,Aguadilla,PR
00604,Aguadilla,PR
00605,Aguadilla,PR
00606,Maricao,PR
00610,Anasco,PR
00611,Angeles,PR
00612,Arecibo,PR
00613,Arecibo,PR
00614,Arecibo,PR
00616,Bajadero,PR
00617,Barceloneta,PR
00622,Boqueron,PR
00623,Cabo Rojo,PR
00624,Penuelas,PR
00627,Camuy,PR
00631,Castaner,PR
..................
48730,East Tawas,MI
48731,Elkton,MI
48732,Essexville,MI
48733,Fairgrove,MI
48734,Frankenmuth,MI
48735,Gagetown,MI
48736,Gilford,MI
48737,Glennie,MI
48738,Greenbush,MI
48739,Hale,MI
48740,Harrisville,MI
48741,Kingston,MI
48742,Lincoln,MI
48743,Long Lake,MI
48744,Mayville,MI
48745,Mikado,MI
48746,Millington,MI
48747,Munger,MI
48748,National City,MI
48749,Omer,MI
48750,Oscoda,MI
48754,Owendale,MI
48755,Pigeon,MI
48756,Prescott,MI
48757,Reese,MI
48758,Richville,MI
48759,Sebewaing,MI
48760,Silverwood,MI
48761,South Branch,MI
48762,Spruce,MI
48763,Tawas City,MI
48764,Tawas City,MI
48765,Turner,MI
48766,Twining,MI
48767,Unionville,MI
48768,Vassar,MI
48769,Tuscola,MI

→ 2 comments Tags: