[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

rails / active record / og: question on tree structures

Its Me

11/21/2004 10:29:00 PM

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).

Thanks!


4 Answers

Sascha Ebach

11/21/2004 11:00:00 PM

0

itsme213 wrote:

> 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-dat... (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:

http://rforum.andreas-s.net/trac/file/rforum/app/mode...

and you wanna look at the database structure here:

http://rforum.andreas-s.net/trac/file/rforum/db/production_str...

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?

--
Sascha Ebach



George Moschovitis

11/22/2004 9:30:00 AM

0

itsme213 wrote:
> 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?

regards,
George.

--
www.navel.gr | tel: +30 2106898050 | fax: +30 2106898437

web appliction engine: http://www.nave...
have fun: http://...

Its Me

11/22/2004 3:52:00 PM

0


"George Moschovitis" <gm@navel.gr> wrote
> > 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.


Tobias Luetke

11/22/2004 6:43:00 PM

0

I might have missunderstood the question, in this case just ignore me.


Some code from my rails app ( AR )

model:
class Category < ActiveRecord::Base
belongs_to :parent, :class_name => 'Category', :foreign_key => 'parent_id'
has_many :children, :class_name => 'Category', :foreign_key =>
'parent_id', :dependent => true
end


@boards, @boots, @powder, @deepsnow are fixtures structured like this :

boards
\_ powder
\_ deepsnow
boots

testcase:

def test_tree

assert_equal true, @boards.has_children?
assert_equal false, @boots.has_children?

assert_equal @boards.children, [@powder, @deepsnow]
assert_equal @deepsnow.parent, @boards

end



On Mon, 22 Nov 2004 07:33:07 +0900, itsme213 <itsme213@hotmail.com> wrote:
> Does rails (or active record) have any way to conveniently define and take
> advantage of the tree structure of containment?

--
Tobi
http://blog.le...