Hi All,

I'm looking to improve finding friends on my site, by adding a
''suggestion'' page/area.

BAsically, a query should be run against a logged in user and find
other people from the same town/location. Or that share a similar
hobby etc.

I have a controller and an index page i want to update this with.

what I have so far:


  def thingsincommon
       @users = User.find_by_user_id(, 1, :limit =>
1, :conditions => "location like 'London%'")


To be honest, this query would only work for thise people who live
near somewhere similar to london. Is it possible to amend it further
to read the user's location (which they add as part of further site
registration) so then the query is looking for other people nearby


    <% if @user.thingsincommon %>

  <% end %>

Thing is, i can't find a tutorial to cover this, so I can see the
example. The api site is handy for controller stuff, but when I want
to render results based on the query, I'm finding it difficult..

Any ideas how to accomplish this?

Kind Regards

I'm not sure I understand your question. Do you need a list of cities
near London that you want to search through? Or you have a problem
with views?


No - sorry my poor explanation..

I have a site that people can join and add friends.

I want to add a feature that suggests friends for them.

So – for example user X has registered and filled in details such as
‘current location’ = London
Fav Movie = ‘Avatar’ etc

I’d like to be able to then use ruby on rails to go off and find other
registered users that have similar or same interests

Hope this is clearer…?

Mm. The problem here is that your problem has little if anything to do with Rails or Ruby even.

You have a database with a list of users and some additional data (such as favourite movie) that, hopefully, lives in a another table.

What you want to find is the common interests / favourites between two users. For a given user, 1234, you could iterate over all their favourites building a list of other users who have similar interests.

So for user 1234 we find a list of other users who have the same location and allocate them a score of 1 (so it’s just a hash with the user id as the key and a value).

We then find out which users in that list also like the same film and add one to that users score, repeating this for all user 1234s favourites and other pertinent data.

Then we pick, for example, the top five highest scoring users and suggest them as possible friends.

The trouble is that this is potentially very expensive. If you have a lot of users the number of people that live in London will be very large. So you will have to compare a lot of data which is slow and expensive. On Facebook I listed five of my favourite musicians, I had to add three of them myself, no one else had ever listed them or probably even knew of them. Of the two that were already on Facebook one was pretty unknown (Mystified) and the final one was Interpol. So matching friends based on my musical choices will result in either one match, the only other Mystified fan on Facebook (assuming that he lives on the same continent as me) or thousands of Interpol fans. Hardly a success.

Then there is the problem of spelling, how many ways are there to spell Avatar (ok there is only one correct way but that doesn’t matter). And how about Shrek 2, if I list my favourite movies as Shrek 2 will I get matched to people who liked Shrek (likely to be a good match), or how about people who entered ‘Shrek Two’ or ‘Shrek II’.

How about Oceans 11, do I mean the 1960 version or the 2001 version. These are in effect two different films.

Then you will have to run this in near real time because people change their minds about what they like so you cannot easily precompute a list of possible friends too far in advance of needing it.

Once you have sorted out an algorithm that can result in a good match and managed to implement it efficiently then how you plug it into a Rails application will have already been solved.

Your problem is the matching algorithm, ignore Rails for the moment and focus on that.


Unless you are a contractor and getting paid to do it :slight_smile:

Thanks for the response.

Slightly more complex than I'd hoped, but certainly makes sense.

Many thanks!