Skip to content

Instantly share code, notes, and snippets.

@domwom
Created April 6, 2009 08:58
Show Gist options
  • Save domwom/90676 to your computer and use it in GitHub Desktop.
Save domwom/90676 to your computer and use it in GitHub Desktop.
Facebook-ish "people you may know" thingie
class User < ActiveRecord::Base
has_many :friends, :through => :friendships
has_many :friendships, :dependent => :destroy
def getMutualFriendsWith user
intersection = friends() & user.friends()
end
def getNumberOfMutualFriendsWith user
getMutualFriendsWith(user).length()
end
def findPeopleMightKnown
pymk = []
for friend in friends()
pymk.push(friend.friends())
end
pymk.flatten!
pymk.uniq!
pymk = (pymk - friends()) - [self]
pymk.sort! { |a,b| b.getNumberOfMutualFriendsWith(self) <=> a.getNumberOfMutualFriendsWith(self) }
pymk.reject! {|x| x.getNumberOfMutualFriendsWith(self) == 0 }
return pymk
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment