# Current\_user.friends.posts

**URL:** <https://www.ruby-forum.com/t/current-user-friends-posts/104943>\
**Category:** Rails\
**Created:** [May 24, 2007, 6:47am UTC](https://www.ruby-forum.com/t/current-user-friends-posts/104943 "2007-05-24T06:47:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_Kroman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f9ae1b/32.png) [@Alex\_Kroman](https://www.ruby-forum.com/u/Alex_Kroman)\
**Post date:** [May 24, 2007, 6:47am UTC](https://www.ruby-forum.com/t/current-user-friends-posts/104943/1 "2007-05-24T06:47:55Z")

</div>

Hi all,

I have a pretty simple set of models.

current\_user has a bunch of friends and each friend makes a lot of  
posts.

I want to do something like current\_user.friends.posts but the posts  
method is missing.

I’ve come up with a bunch of ways to accomplish this but nothing looks  
very good. What’s the most elegant way to do it?

Thanks,  
Alex

---

<div class="post-metadata">

**Author:** ![Alex\_Kroman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f9ae1b/32.png) [@Alex\_Kroman](https://www.ruby-forum.com/u/Alex_Kroman)\
**Post date:** [May 24, 2007, 6:57am UTC](https://www.ruby-forum.com/t/current-user-friends-posts/104943/2 "2007-05-24T06:57:45Z")

</div>

Create the posts method yourself by extending the association:

has\_many :friends do  
def posts  
#get posts here  
end  
end

See the Association Extensions in the api docs for a few variables that  
you might need to make that work.

Alex Kroman wrote:

> Hi all,
> 
> I have a pretty simple set of models.
> 
> current\_user has a bunch of friends and each friend makes a lot of  
> posts.
> 
> I want to do something like current\_user.friends.posts but the posts  
> method is missing.
> 
> I’ve come up with a bunch of ways to accomplish this but nothing looks  
> very good. What’s the most elegant way to do it?
> 
> Thanks,  
> Alex

---

<div class="post-metadata">

**Author:** ![Alex\_Kroman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f9ae1b/32.png) [@Alex\_Kroman](https://www.ruby-forum.com/u/Alex_Kroman)\
**Post date:** [May 24, 2007, 3:45pm UTC](https://www.ruby-forum.com/t/current-user-friends-posts/104943/3 "2007-05-24T15:45:44Z")

</div>

Thanks so much for your reponse.

Right now I have the following code:

has\_many :friendships\_by\_me,  
:foreign\_key =\> ‘user\_id’,  
:class\_name =\> ‘Friendship’ do  
def posts  
Post.find(:all,  
:include =\> {:user, :friendships},  
:conditions =\> [‘friendships.user\_id = ?’, self.id])  
end

which should work but self.id is returning the id of the Friendship  
class. How do I get it to return the id of the user?

Thanks,  
Alex

On May 23, 9:57 pm, Gabe Da silveira \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![Alex\_Kroman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f9ae1b/32.png) [@Alex\_Kroman](https://www.ruby-forum.com/u/Alex_Kroman)\
**Post date:** [May 24, 2007, 5:41pm UTC](https://www.ruby-forum.com/t/current-user-friends-posts/104943/4 "2007-05-24T17:41:34Z")

</div>

Awesome!

What worked for me was:

has\_many :friends do  
def posts  
map(&:posts)  
end  
end

That is so much nicer looking then my first attempt.

---

<div class="post-metadata">

**Author:** ![Alex\_Kroman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f9ae1b/32.png) [@Alex\_Kroman](https://www.ruby-forum.com/u/Alex_Kroman)\
**Post date:** [May 24, 2007, 5:03pm UTC](https://www.ruby-forum.com/t/current-user-friends-posts/104943/5 "2007-05-24T17:03:21Z")

</div>

This little one liner should do it for you, if I’m not mistaken.

current\_user.friends.map(&:posts)

which is equivalent to:

current\_user.friends.map{|f| f.posts}

Tyler
