Does rails (or active record) have any way to conveniently define and take
advantage of the tree structure of containment?
e.g. a Modules contains Name, Modules and Classes.
A Class contains Name, Attributes.
An Attribute contains a Name and a Type.
The tree-like containment relations can be exploited more (certainly
differently) from non-tree relationships (e.g. a Module imports other
Modules, a Class inherits a Class).
> Does rails (or active record) have any way to conveniently define and take
> advantage of the tree structure of containment?
>
> e.g. a Modules contains Name, Modules and Classes.
>
> A Class contains Name, Attributes.
>
> An Attribute contains a Name and a Type.
>
> The tree-like containment relations can be exploited more (certainly
> differently) from non-tree relationships (e.g. a Module imports other
> Modules, a Class inherits a Class).
Funny, that's what I was thinking about today, too. By managing tree structures you probably mean all kinds of hierarchical data and the ways this data gets managed and stored in the database. There are usually 2 ways of doing this. The "The Adjacency List Model" and the "Modified Preorder Tree Traversal". Both nicely explained in this article: http://www.sitepoint.com/print/hierarchical-data-database (with php as its example language)
I was asking on #rubyonrails IRC-channel today, if there was an implementation in Ruby I could learn from, because I am working on a CMS which stores its data in tree form, much like a forum. Someone pointed me to RForum, which is developed using Rails. Their source code reads nicely (so, nice that I think I going to borrow some of it RForum uses a variation of the Preorder Tree Traversal algo. You can see how it works here:
But back to your question. I think it would be great if there was support for this as a module (if this can be a module/library to Rails). Actually coding something something with trees always costs me a lot of time (when coding cmss or forumsoftware and the likes) and it would be great if this kind of functionality could simply be dropped into your Rails app. At the moment it just looks like I am going to copy some of their code and customize it to my purposes. I have barely thought about how to generalize this to make a (datastore) plugin out of it. Maybe someone else already has?
> Does rails (or active record) have any way to conveniently define and
take
> advantage of the tree structure of containment?
I dont understant what you want to do, can you explain?
Macro(s) that roughly do the following:
class House
contains_many :attr=>:rooms, :class=>Room, :adder=>:add_room,
:local_key=>:name
end
Automated ways to
- create rooms within context of house
- delete rooms when house is deleted
- persist rooms as part of persisting house (e.g. xml tree)
- enforce: object can only be contained within 1 other object
- provide lookup for room by local_key :name within house
(room's local key = name, global key = house_id + name)
- recursively provide tree-wide lookup
(e.g. to establish non-tree links: e.g. xml id_refs)
- view rooms within view of house
- variations to allow vs. prohibit transfer of rooms
- etc.