# Question on how to form this query elegantly

**URL:** <https://www.ruby-forum.com/t/question-on-how-to-form-this-query-elegantly/182143>\
**Category:** Rails\
**Created:** [January 23, 2010, 10:11am UTC](https://www.ruby-forum.com/t/question-on-how-to-form-this-query-elegantly/182143 "2010-01-23T10:11:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![palidanx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/97f17d/32.png) [@palidanx](https://www.ruby-forum.com/u/palidanx)\
**Post date:** [January 23, 2010, 10:11am UTC](https://www.ruby-forum.com/t/question-on-how-to-form-this-query-elegantly/182143/1 "2010-01-23T10:11:10Z")

</div>

Hi everyone,

I have a model like the following  
User.id

Friends  
invited (fk to user)  
inviter (fk to user)

Now, what I would like to do is given a uid, retrieve all the users  
related to the inviter. So a retarded way of oding hti sis…

friendRequests = Friend.find(:all, :conditions =\> {:invited\_id =\>  
uid})

Then iterate through the friend requests, grabbing each user object…

I know there is a better way of doing this in one query… can I have  
some direction on doing this?

thanks!  
-dan

---

<div class="post-metadata">

**Author:** ![palidanx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/97f17d/32.png) [@palidanx](https://www.ruby-forum.com/u/palidanx)\
**Post date:** [January 23, 2010, 11:24am UTC](https://www.ruby-forum.com/t/question-on-how-to-form-this-query-elegantly/182143/2 "2010-01-23T11:24:19Z")

</div>

On 22 January 2010 21:24, palidanx [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> related to the inviter. So a retarded way of oding hti sis…
> 
> friendRequests = Friend.find(:all, :conditions =\> {:invited\_id =\>  
> uid})
> 
> Then iterate through the friend requests, grabbing each user object…
> 
> I know there is a better way of doing this in one query… can I have  
> some direction on doing this?

I don’t understand fully the associations between the models, how have  
you setup the has\_many, belongs\_to etc.

Can you be more explicit about what you mean by ‘all the users related  
to the inviter’?

Colin
