First of all, if you''re doing what I think you''re doing, you
want to
have a look at acts_as_tree (search the api docs...). It will provide
a slightly more consistent interface fo representing a tree that
(among other things), allows multiple roots.
Secondly, when representing a recursive structure, using recursive
calls is perfectly acceptable. Now, if your trees are so large that
you''re blowing the stack (hundreds of levels deep), then you need to
get more creative and find an iterative solution, but what you''re
doing looks fine to me.
Brian
On 4/22/05, James <james-DkjEhSRHeDJAFePFGvp55w@public.gmane.org>
wrote:> Has anyone done a menu similar to this using Rails:
>
> I have a table with columns pages.id, pages.page_id, pages.title, etc.
> I did this previously in PHP using a function which called itself. I
> guess what I''m wondering is if there is an easier way to do this
using
> Ruby, or if I do need to resort to my old ways and have a method call
> itself like this:
>
> def menu( pages = Page.find(:all, :conditions => "page_id =
0") )
> pages.each do |page|
> ...
> menu(page.pages) if !page.pages.empty?
> ...
> end
> end
>
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
--
The years ahead pick up their dark bags.
They move closer. There''s a slight rise in the silence
then nothing.
--
(If you''re receiving this in response to mail sent to
bluczkie-OM76b2Iv3yLQjUSlxSEPGw@public.gmane.org, don''t be concerned
This is my new address,
but mail will be forwarded here indefinitely)