# Query Help

**URL:** <https://www.ruby-forum.com/t/query-help/165121>\
**Category:** Rails\
**Created:** [April 9, 2009, 10:42am UTC](https://www.ruby-forum.com/t/query-help/165121 "2009-04-09T10:42:10Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![newb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/2acd7d/32.png) [@newb](https://www.ruby-forum.com/u/newb)\
**Post date:** [April 9, 2009, 10:42am UTC](https://www.ruby-forum.com/t/query-help/165121/1 "2009-04-09T10:42:10Z")

</div>

In my App …i have Userinfo and FriendsInfo table.Each user has many  
friends.those friends are user of the app.  
for example i have 3 users in my userinfo table.namely  
=\>John  
=\>Victor  
=\>Masvi

the users can add other users as friends.the user john has friends as  
victor and masvi.these information i have stored in my FriendsInfo  
table.  
friendstable structure  
id user\_id friend\_id  
1 1 2  
1 1 3

if i want to find who are friends of the john…  
i do query like this @friends = Userinfo.find(:all,:conditions =\>  
{:user\_id =\> params[user\_id]})  
so i will be getting all the friends id of john(his id is 1)  
the @friends result set is array.  
if i want display the friends name of john means  
i do query like this  
@friends\_name = Array.new  
@friends.each do |f|  
@name = Userinfo.find\_by\_id(f.friend\_id)  
@friends\_name \<\< @name  
end

how can i optimise my queries to get the above friends name…

pls help
