David Roy
2011-Sep-11 22:10 UTC
How to recursively count threads belonging to a chain of forums
class Forum::Forum < ActiveRecord::Base
belongs_to :parent_forum, :class_name => "Forum"
has_many :sub_forums, :class_name => "Forum", :foreign_key =>
:parent_id
has_many :threads
def count_threads
threads.count
end
end
Hi I am trying to count all the threads that belong to a forum right
though the chain. Currently it counts the threads that belong to the
forum you''re on, so if you''re on forum id 1 it will count
threads that
belong to forum id 1 only, however forum id 1 also has sub_forums such
as forum id 4 which also has a sub_forum with a id of 8 and this could
go on forever.
I would really appreciate some help here, I have grand plans to build my
site in rails instead of zend framework but a few little snags are
keeping me stuck sometimes.
--
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Pavling
2011-Sep-11 22:27 UTC
Re: How to recursively count threads belonging to a chain of forums
On 11 September 2011 23:10, David Roy <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi I am trying to count all the threads that belong to a forum right > though the chain. > > class Forum::Forum < ActiveRecord::Base > belongs_to :parent_forum, :class_name => "Forum" > has_many :sub_forums, :class_name => "Forum", :foreign_key => > :parent_id > has_many :threads >def count_threads sub_forums.inject(threads.count) { |memo, sub_forum| memo +sub_forum.count_threads } end> endThat should recurse through all the sub-forums... make sure you don''t nest an upper forum in one of the children because you''ll get a stack overflow :-) -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
David Roy
2011-Sep-11 23:08 UTC
Re: How to recursively count threads belonging to a chain of forums
Cheers was a great help -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.