# Find\_by

**URL:** <https://www.ruby-forum.com/t/find-by/182725>\
**Category:** Rails\
**Created:** [February 1, 2010, 11:32pm UTC](https://www.ruby-forum.com/t/find-by/182725 "2010-02-01T23:32:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 1, 2010, 11:32pm UTC](https://www.ruby-forum.com/t/find-by/182725/1 "2010-02-01T23:32:59Z")

</div>

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:

Controller:

def thingsincommon  
@users = User.find\_by\_user\_id( @user.id, 1, :limit =\>  
1, :conditions =\> “location like ‘London%’”)

end

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  
etc?

Html.erb:

```
<% 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

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 1, 2010, 11:58pm UTC](https://www.ruby-forum.com/t/find-by/182725/2 "2010-02-01T23:58:09Z")

</div>

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?

Regards,  
Milan

On Feb 1, 11:30 pm, RubyonRails\_newbie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 2, 2010, 9:54am UTC](https://www.ruby-forum.com/t/find-by/182725/3 "2010-02-02T09:54:33Z")

</div>

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…?

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 2, 2010, 10:58am UTC](https://www.ruby-forum.com/t/find-by/182725/4 "2010-02-02T10:58:22Z")

</div>

Thanks for the response.

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

Many thanks!

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 2, 2010, 10:46am UTC](https://www.ruby-forum.com/t/find-by/182725/5 "2010-02-02T10:46:39Z")

</div>

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.

Better yet, THE WORLD DOES NOT NEED YET ANOTHER DATING  
SITE!!!

Unless you are a contractor and getting paid to do it 🙂
