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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
A1247f73598357f06388e863c41aa624?d=identicon&s=25 Elias Orozco (elioncho)
on 2009-02-12 23:58
(Received via mailing list)
Hello,

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?

Thanks,

Elias
E18bd905923970b71d64c4b70000f055?d=identicon&s=25 patfrat (Guest)
on 2009-02-13 11:29
(Received via mailing list)
Hello,
I manage a database to map directories too ...
I use the plugin better_nested_set :
http://wiki.rubyonrails.org/rails/pages/BetterNestedSet
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.