On Sep 1, 5:42 pm, Jon Hope <[email protected]> wrote:
> What I want is to be able to return all the forked_stories and their
> forked_stories for a given Story and I'm finding this very difficult
> since they're all based on the same model. I keep running into dead
> ends. If I can return them to a certain depth as well that'd be great,
> so I've been trying to write a method that accepts a depth parameter but
> the crazy loops are frying my brain.
As far as I can tell you have an algorithmical difficulty, not one
related to Rails. To get back all the forked stories for a given story
the following code snippet would do the job:
def fork_tree
forked = forked_stories
forked_from_children = forked.inject([]) do |fstories, fstory|
fstories + fstory.fork_tree
end
forked + forked_from_children
end
(you can see the whole thing here: http://gist.github.com/179186)
Note1: This has been tested under Ruby, not Rails
Note2: The method is badly named, since it just returns an array, not
a tree-like structure of all the stories forked directly from the
story or from its children, or grandchildren, etc.
Hope this helps,
Bálint
--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---