# Find all messages from all friends

**URL:** <https://www.ruby-forum.com/t/find-all-messages-from-all-friends/162932>\
**Category:** Rails\
**Created:** [March 15, 2009, 10:27pm UTC](https://www.ruby-forum.com/t/find-all-messages-from-all-friends/162932 "2009-03-15T22:27:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [March 15, 2009, 10:27pm UTC](https://www.ruby-forum.com/t/find-all-messages-from-all-friends/162932/1 "2009-03-15T22:27:25Z")

</div>

Hi,

I have an app where users have friends, and friends can have messages  
for friends to see. I would like to list all the messages that my  
friends have easily.

I can use @user\_friends = current\_user.friends to find all my friends  
and then I could loop through the friends to get each friend’s messages  
to place them in some array of messages I guess.

But, is there a simpler way to get that list of messages?

(I’m using has\_many\_friends plugin btw)

Thanks.

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [March 16, 2009, 11:13am UTC](https://www.ruby-forum.com/t/find-all-messages-from-all-friends/162932/2 "2009-03-16T11:13:37Z")

</div>

Message.all(:conditions =\> {:user\_id =\> current\_user.friends})

assuming the table messages has a column user\_id.

On Mar 15, 10:27 pm, comopasta Gr [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [March 16, 2009, 9:31pm UTC](https://www.ruby-forum.com/t/find-all-messages-from-all-friends/162932/3 "2009-03-16T21:31:10Z")

</div>

“Wolas!” wrote:

> Message.all(:conditions =\> {:user\_id =\> current\_user.friends})
> 
> assuming the table messages has a column user\_id.
> 
> On Mar 15, 10:27ï¿½pm, comopasta Gr [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Thanks Wolas, actually that worked fine 🙂  
I’m a bit worried about the performance side of that request since there  
can be a huge number of messages and users. Of course user\_id should be  
indexed in that table but I’m still worried.

Any comments on the performance from anyone?

Thanks.

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [March 16, 2009, 11:47am UTC](https://www.ruby-forum.com/t/find-all-messages-from-all-friends/162932/4 "2009-03-16T11:47:42Z")

</div>

> Message.all(:conditions =\> {:user\_id =\> current\_user.friends})

as i see it that would give you an error (as current\_user.friends is  
an array not an integer).

nonestly i’d do it with a loop. and why not? it’s easy and readable.

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [March 16, 2009, 10:16pm UTC](https://www.ruby-forum.com/t/find-all-messages-from-all-friends/162932/5 "2009-03-16T22:16:27Z")

</div>

> Any comments on the performance from anyone?

Don’t worry about it until it’s a problem. YAGNI.

But then again, you can do caching, paginate, intelligently use ajax  
to load only what’s needed, etc.

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [March 17, 2009, 9:06am UTC](https://www.ruby-forum.com/t/find-all-messages-from-all-friends/162932/6 "2009-03-17T09:06:08Z")

</div>

> Thanks Wolas, actually that worked fine 🙂

wow, i just learned something. ignore everything i said so far. i  
have to try that immediately…

---

<div class="post-metadata">

**Author:** ![comopasta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@comopasta](https://www.ruby-forum.com/u/comopasta)\
**Post date:** [March 17, 2009, 11:49am UTC](https://www.ruby-forum.com/t/find-all-messages-from-all-friends/162932/7 "2009-03-17T11:49:16Z")

</div>

> I’m a bit worried about the performance side of that request since there  
> can be a huge number of messages and users. Of course user\_id should be  
> indexed in that table but I’m still worried.

this is still teh fastest way to do it since you are telling the db  
fetch all of these ids at once.

Then again,

> Don’t worry about it until it’s a problem

is kinda the best advice in the matter if you add. “or untill you are  
finished, have done profiling and are bored of tv”

On Mar 16, 9:31 pm, comopasta Gr [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
