# Query problem

**URL:** <https://www.ruby-forum.com/t/query-problem/179399>\
**Category:** Rails\
**Created:** [November 24, 2009, 5:48am UTC](https://www.ruby-forum.com/t/query-problem/179399 "2009-11-24T05:48:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gandhi-tush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@gandhi-tush](https://www.ruby-forum.com/u/gandhi-tush)\
**Post date:** [November 24, 2009, 5:48am UTC](https://www.ruby-forum.com/t/query-problem/179399/1 "2009-11-24T05:48:02Z")

</div>

Hi,  
I am using postgres sql.  
I am writing following query to get the user friends.  
The query is working fine.

“User.fnd (:all,:joins =\> “INNER JOIN friendship\_requests fr on  
(users.id = fr.friendship\_target\_id or users.id= fr.user\_id)”,  
:conditions =\> [’(fr.user\_id = ? or fr.friendship\_target\_id= ?) and  
users.id != ? AND fr.status = ?’, self.id,self.id, self.id,  
‘accepted’])”

The query only returns me the user object, but I want the  
friendship\_requests object as well.

Can anyone tell how should I modify this query so that I will get the  
both users as well as friendship\_requests object.

Thanks,  
Tushar

---

<div class="post-metadata">

**Author:** ![gandhi-tush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@gandhi-tush](https://www.ruby-forum.com/u/gandhi-tush)\
**Post date:** [November 24, 2009, 6:08pm UTC](https://www.ruby-forum.com/t/query-problem/179399/2 "2009-11-24T18:08:10Z")

</div>

You should avoid using big chunks of sql like this. You haven’t  
specified your schema, and i’m afraid i don’t feel like reverse  
engineering your sql at the moment to work out what you’re trying to do.

If you use the :find option instead of :joins you can write something a  
bit nicer like this

@users = User.find(:all, :include =\> [:friendship\_requests])

Then in your view you can iterate over the friendship requests for each  
user. Or whatever, like i say with no explanation of what you’re trying  
to do it’s hard to advise clearly.
