I''m developing a system similar to the ForumExample (http:// wiki.rubyonrails.org/rails/pages/ForumExample), and stumbled upon the following. Let''s say I wanted to get all the categories a certain user posted in. How do I get that information withouth using a custom query through :finder_sql (and thus losing the find_in_collection ownage)? It would also be great to see how many post that author made in a category while I''m at it. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Not tested, not sure if it works, but an idea: #Controller @categories = Category.find :all, :conditions => "(messages.author_id = 1 AND categories.id = 1", :include => { :forums => { :topics => replies } } #view <% @categories.each do |c| posts = 0 topics = 0 topics = c.forums.inject do |tsum,topic| tsum += 1 #count and sum up all topics user has created. posts += topic.inject do { |psum,post| psum += 1 } #count and sum up all posts of the user in each topic end end %> <%= "#{@user.id} has started #{topics} Topics and #{posts} in Category #{c.name}" %><br /> <% end %> not sure if i use inject correctly here, just googled this thing :D Hope oyu get the idea. and you should put thta stuff in a helper i guess, to make it less clumpsy. On 8 Feb., 12:07, "Robert" <robertg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m developing a system similar to the ForumExample (http:// > wiki.rubyonrails.org/rails/pages/ForumExample), and stumbled upon the > following. > > Let''s say I wanted to get all the categories a certain user posted in. > How do I get that information withouth using a custom query > through :finder_sql (and thus losing the find_in_collection ownage)? > It would also be great to see how many post that author made in a > category while I''m at it.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Eager loading would be the answer: Category.find(:all, :include=>[{:forums=>{:messages=>:author}}], :conditions=>[''authors.name= ?'', ''Robert'']) I''m not sure this needs corrections or not but the trick is that curly braces in :include gives you nested joins. Charly On 8 fév, 12:07, "Robert" <robertg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m developing a system similar to the ForumExample (http:// > wiki.rubyonrails.org/rails/pages/ForumExample), and stumbled upon the > following. > > Let''s say I wanted to get all the categories a certain user posted in. > How do I get that information withouth using a custom query > through :finder_sql (and thus losing the find_in_collection ownage)? > It would also be great to see how many post that author made in a > category while I''m at it.--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
The find() works (although I must rather would like to have a thing like this in the model). Counting the posts is still a problem. An each() loop isn''t that scalable. Any thoughts on doing a SUM in the SQL query somewhere or something? I''ve tried out a couple of things with :select, but it was just ignored... On Feb 8, 2:06 pm, "Charly" <charlysi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Eager loading would be the answer: > Category.find(:all, :include=>[{:forums=>{:messages=>:author}}], :conditions=>[''authors.name= ?'', > ''Robert'']) > I''m not sure this needs corrections or not but the trick is that curly > braces in :include gives you nested joins. > > Charly > > On 8 fév, 12:07, "Robert" <robertg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I''m developing a system similar to the ForumExample (http:// > > wiki.rubyonrails.org/rails/pages/ForumExample), and stumbled upon the > > following. > > > Let''s say I wanted to get all the categories a certain user posted in. > > How do I get that information withouth using a custom query > > through :finder_sql (and thus losing the find_in_collection ownage)? > > It would also be great to see how many post that author made in a > > category while I''m at it.--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---