Forum: Ruby on Rails Building a directory tree into the database

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Elias O. (Guest)
on 2009-02-13 00:58
(Received via mailing list)

Does anyone can give me some pointers on building a directory tree. I
want to map a directory of folders and files into a database. I am not
sure how to do this dynamically. I think acts_as_tree could help me do
this. Any ideas?


patfrat (Guest)
on 2009-02-13 12:29
(Received via mailing list)
I manage a database to map directories too ...
I use the plugin better_nested_set :
You can mange roots and children easily with this.
When you get, for example, a directory containing others directories
and files,
you can add the first directory as root and subdirectories and files
as children of root... with "child.move_to_child_of root"
When you want to get children for the root directory with your model :
"root.children" or "child.children" etc ...
And to get the parent directory : "chid.parent" or "child.ancestors"
etc ...
You just have to parse the directory tree with Dir.entries or anything
else and add the results to the database using your model containing
acts_as_nested_set and saving each entries using move_to_child_of
method if it's needed.
This topic is locked and can not be replied to.