Archive for June, 2009

Zip Code Distance Searching in Ruby on Rails

Tuesday, June 23rd, 2009

In my last entry we looked at using scopes to dynamically build scopes for searching in your Ruby on Rails application.  In Tryst, we needed to let our uses search for other singles that were xxx miles from their location.  We require that new users enter their zip code, so that is what we had to go off of.

When I was researching the best way to do this, I did a quick Twitter poll – most of my friend thought I should just use Google’s GeoCoding API, which will let you preform these types of searches.  I really didn’t want to go this route for a couple reasons.

  1. It’s an external dependency.  Yes, Google’s uptime is impeccable, but I don’t want to rely on them
  2. Google’s TOS requires that the site/app/service be free.  Tryst may end up with paid features down the road and I didn’t want to have to rewrite a substantial amount of code 1 year from now.
  3. It’s a challenge.

So, we decided to do our own thing.

Our workflow for a search would work like this – we get a distance and a zip code as search parameters, feed both of these into something that returns all the zip codes within the specified distance, and then find all the uses in those zip codes.  Easy enough.

PS. I’m using MySQL here.

First, we need a reference for zip codes.  I found this great free CSV that has all zip codes, major cities, longitude, latitude, area codes, and even metropolitan codes. Import it into your database and remove any rows that were not in the US.  I actually had a ‘Location’ model in my Rails application that I imported this into.

After some digging around the webs, I found some crazy algorithms for generating distance between different locations based on their longitude and latitude.  I’m not going to pretend to understand everything that is happening here, but after some playing around I ended up with this:

SELECT o.zip_code
FROM locations z, locations o, locations a

WHERE z.zip_code = #{zip_code}
AND	z.zip_code = a.zip_code
AND	(3956 * (2 * ASIN(SQRT(
		POWER(SIN(((z.latitude-o.latitude)*0.017453293)/2),2) +
		COS(z.latitude*0.017453293) *
		COS(o.latitude*0.017453293) *
		POWER(SIN(((z.longitude-o.longitude)*0.017453293)/2),2)
	)))) <= #{distance}

I’ll let you guess which part does the calculation. :)
zip_code and distance you’ll need to pipe into the SQL, but you’ll get back all the zip codes with in your specification. We just thew the returned array of zip odes into a scope like this:

# models/users.rb
def search(params)
    scope = User.scoped({})
    ...
    scope = scope.conditions "users.zip_code in (?)", zip_codes unless zip_codes.blank?
    ...
end

Now, this query is fairly slow. By slow, I mean about 25-30ms correctly indexed on my development box. For us it’s just fine.

I hope that helped someone out – I had a hell of a time finding a solution I liked and I really think this is the best one.

Smart Searching Using Anonymous Scopes

Sunday, June 21st, 2009

I really like how Lovetastic’s search works.  Instead of a plethora of select boxes, users can simply use expressions to create searches.  We did something similar with Tryst’s searching.  One text field and you enter stuff like age ranges (30-45), things like smoke-yes or smoke-no, cities, zipcodes, etc.

Tryst.com Search

To make this whole thing more Rails like, I did not want to just use Regex to parse out search terms and plug them into a complicated SQL query.  That would be soooooo PHP.

Named Scopes are awesome.  You can use them to built decently formatted and fairly complicated SQL queries in a nice, no bullshit, manner. You can do cool stuff like this:

@users = User.active.males.in_zipcode(params[:zip_code]).nonsmokers.top_ten

Just keep stacking scopes on.  Rails generates the query and it usually doesn’t suck.

Go read the API docs for the basics on Named Scopes. We’re gonna do some not basic stuff. Check out Ryan Bate’s Anonymous Scopes Railscast, which introduced me to this method of generating Scopes.

We wanted to add a Class Method called search and have it define the Scopes. We pass in the params hash, anything else we need (like the current user), and let it handle the Scoping and determining what we’re looking for from the search params.

To start, here is our search class method.

# app/models/user.rb
 def self.search(params, current_user)
    page = params[:page] || 1
    search_terms = params[:search]
end

Yeah, using Will Paginate and assigning my search terms to a local variable. I’ve also written another Class Method that does the parsing of the params.

# app/models/user.r.b
def self.search(params, current_user)
    page = params[:page] || 1
    search_terms = params[:search]
   #parse params
    max_age, min_age, zip_codes, gender, smoke, drink = self.get_parameters(search_terms.to_s, current_user)
  end

Now, we’re going to define some dynamic scopes. This is awesome. For it to work, we’ll add an initializer.

#config/initializers/scopes.rb

class ActiveRecord::Base
  named_scope :conditions, lambda { |*args| {:conditions => args} }
end

Now, lets make the scopes.

def self.search(params, current_user)
    page = params[:page] || 1
    search_terms = params[:search]

    max_age, min_age, zip_codes, gender, smoke, drink = self.get_parameters(search_terms.to_s, current_user)

    scope = User.scoped({})
    scope = scope.conditions "users.age >= ? and users.age <= ?", min_age, max_age unless min_age.blank?
    scope = scope.conditions "users.zip_code in (?)", zip_codes unless zip_codes.blank?
    scope = scope.conditions "users.smoker = ?", smoke unless smoke.blank?
    scope = scope.conditions "users.drink = ?", drink unless drink.blank?
    scope = scope.conditions "users.gender = ?", gender unless gender.blank?
    scope.paginate :page => page, :order => "created_at DESC"
  end

Pretty slick, huh? So now, in my controller I just need

# app/controllers/search.rb
def users
    @users = User.search(params, current_user)
end

Assuming your class method for parsing out scope parameters doesn’t suck, you should have very clean, cuddly, and concise searching. No more worrying about breaking some huge crappy SQL query.

You’ll notice I used

users.zip_codes in (?)

This is because I can pass an array of zipcodes and return all the users in those zipcodes. This is because I’ve implemented radial distance searching base on zipcodes. Users can search like ” 60606 25miles” and return all users who are within 25 miles of 60606. We’ll go into how this works in the next post.

Yes, I started a dating site. I’m sorry.

Sunday, June 14th, 2009

Ok, so five years ago I never would have though that I’d have started a ‘Web 2.0′ dating site.  Hell, I didn’t think I’d be doing a lot of things, but here I am.   There is actually a small story how I got involved with (shamless self promotion) trystme.com.  Wanna hear it?  Good.

Originally, I wanted to become an elementary school teacher and was halfway though getting my elementary eduction BA… until I started to volunteer at a school library.  I hated it.  Plus I realised I had too many tattoos to be a school teacher… never should have been in all those punk bands growing up.

So there went that idea – I quit school and took a year off.

I decided to go for something that is completely different and the opposite of little crazy kids – computer science.  I had always like computers, had some experience with Dreamweaver and the idea of not having my work talk back to me all the time sounded great.  I went back to school and loved CS.

About 3 years ago I graduated and I got a contract job with a small local development shop in Duluth, MN.  After a few months it went under and the guy screwed me out of a pay check or two, but I did walk away with something really cool – I was exposed to Ruby on Rails.  I freakin loved it.  LOVED IT.  I quit wasting my time with PHP and drank the Ruby juice.  I even wrote an article for a Rails magazine.

Eventually I got a job working programming for this educational coop in Norther Minnesota.  It was pretty slick.  I enjoyed the work, had tons of freedom, and actually got paid (unlike the contract gig).  Worked there for a little over a year… until… I got an iPhone January 2nd of 2009.

I had a couple week lapse in my old phone contact, so my old phone was still on but it would get left at home.  One day it rang and my wife answered it.  It was some lady from Chicago who said she worked for some company that was trying to find Rails developers and somewhere they found my resume or something crazy like that.  She even said that she had called before and left messages and that I never called back.  This is true – I rarley call back if I don’t know the caller.  Thanks telemarketers.

So, Jen tells me I should call this lady, Andrea The HR Lady From Chicago.  Jen and I had been talking about moving from Norther MN for sometime now and almost took a job in Milwaukee (thank God we didn’t).  The more I talked to Andrea The HR Lady From Chicago the more it sounded good.

So I got this job, moved to the Chicago Burbs, and met a guy at work named Mike.  Mike had lots of crazy ideas and had this one to start a dating site.  The original idea was to only spend two weeks developing it and whatever we had at the end of two weeks was it.  This sounded great at first, but since two weeks really meant about 40 hours between the two of us (since we’re doing this at night after our real jobs) it got stretched to about a month and a half.

There you have it.  We didn’t exactly build it to make tons of money. That’d be nice – we have ads on it, but really we just wanna see what happens.  I guess we want what every programmer with an entrepenurial spirit wants – to wake up one morning and find that your one lowely server is dying under the load and we have to split into an app server and a database server.  Then add Memcached, another app server, shard the database, another app server, hire a real DBA, and on and on.

Check it out – we launched it today, 14/6/2009 at 7:30pm, and are super proud of it.  If you’re looking for a date sign up and give it a try.  Let us know what you think and have fun.