i brute force this, is there a cleaner way>?? @friend_ids = Favorite.find(:all, :conditions => ["user_id (#{current_user.id}) and favorable_type = ''User''"]) @friend_blogs = Array.new @friend_ids.each do |x| @friend_blogs << BlogPost.find(:all, :conditions =>["user_id = ?", x.favorable_id],:order => "created_at DESC", :limit => 5) end if like to get only the latest 5 posts of all the users that is friends with current user. thanks -- 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 -~----------~----~----~----~------~----~------~--~---
I would try combining the two searches, either by writing the sql yourself or subtituing user in(?) and doing a find there. You could do it with the :include tag but I haven''t used that yet, if you need to google search its called eager loading WW On Nov 30, 8:19 am, Koloa Poipu <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> i brute force this, is there a cleaner way>?? > > @friend_ids = Favorite.find(:all, :conditions => ["user_id > (#{current_user.id}) and favorable_type = ''User''"]) > > @friend_blogs = Array.new > > @friend_ids.each do |x| > @friend_blogs << BlogPost.find(:all, :conditions =>["user_id = ?", > x.favorable_id],:order => "created_at DESC", :limit => 5) > end > > if like to get only the latest 5 posts of all the users that is friends > with current user. > > thanks > -- > 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 -~----------~----~----~----~------~----~------~--~---
Does something like this work for you? @friends Favorite.find_all_by_user_id_and_favorable_type(current_user.id, ''User'') @friend_blogs = @friends.blog_posts.find( :all, :order => "created_at DESC", :limit => 5) On Nov 30, 8:19 am, Koloa Poipu <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> i brute force this, is there a cleaner way>?? > > @friend_ids = Favorite.find(:all, :conditions => ["user_id > (#{current_user.id}) and favorable_type = ''User''"]) > > @friend_blogs = Array.new > > @friend_ids.each do |x| > @friend_blogs << BlogPost.find(:all, :conditions =>["user_id = ?", > x.favorable_id],:order => "created_at DESC", :limit => 5) > end > > if like to get only the latest 5 posts of all the users that is friends > with current user. > > thanks > -- > 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 -~----------~----~----~----~------~----~------~--~---
I dont like the first query, it would fall into method missing and that takes alot of overhead to create it. I would instead write it as. @friends = Favorite.find(:all, :conditions => ["user_id = ? and favorable_type = ''User'' " , current_user.id) WW --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
The overhead of method_missing would be greatly outweighed by the cost of the database query. Premature optimization is the root of all evil. ///ark --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---