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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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- s.net> wrote:> 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 > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On May 24, 12:46 am, Alex Kroman <alexkro...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. On May 24, 7:19 am, Tyler McMullen <tbmcmul...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 > > On May 24, 12:46 am, Alex Kroman <alexkro...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---