ZipCode Search

I’ve looked through the messages regarding implementing a ZipCode search
and I have a few questions to make sure I have my head wrapped around
this.

Here’s what I have. I have a database table called zipcodes with the
following info (for example)

30310 Atlanta GA 33.727807 -84.41983 -5 1

That’s zip, city, state, latitude, longitude, timezone, and dst.

So when a user comes to my site and they enter in an zip code, I want it
to find all stores within a 20 mile radius of the zip code entered.

The database also includes a table called stores. Address, City, State,
and Zip Code.

The SQL I’ve found to do this is as follow:

@places = Place.find_by_sql ["select p.* from places p where ((3963.0

  • acos(sin(p.latitude/57.2958) * sin(?/57.2958) +
    cos(p.latitude/57.2958) * cos(?/57.2958) * cos(?/57.2958 -
    p.longitude/57.2958))) < ?)", lat, lat, lon,
    distance]

I’m assuming I just modify this SQL to select all the store information
from the database table stores and join it on the zip code database
table using the zip code table latitude, longitude, and 20 (for
distance). Right?