# How-to: Hierarchical model object: Object with relation to i

**URL:** <https://www.ruby-forum.com/t/how-to-hierarchical-model-object-object-with-relation-to-i/91420>\
**Category:** Rails\
**Created:** [April 8, 2007, 8:38pm UTC](https://www.ruby-forum.com/t/how-to-hierarchical-model-object-object-with-relation-to-i/91420 "2007-04-08T20:38:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 8, 2007, 8:38pm UTC](https://www.ruby-forum.com/t/how-to-hierarchical-model-object-object-with-relation-to-i/91420/1 "2007-04-08T20:38:06Z")

</div>

Hi all please advice me on the ‘proper’ way to handle the questions  
below. Any insight, best-practise or hint is very welcome. I use Ruby  
for a while, but Rails is new to me.

==Initial code:  
===My DB contains:  
CREATE TABLE `posts` (  
`id` int(10) unsigned NOT NULL auto\_increment,  
`name` varchar(50) NOT NULL,  
`parent` int(10) unsigned default NULL,  
`description` varchar(255) NOT NULL,  
PRIMARY KEY (`id`),  
UNIQUE KEY `name` (`name`)  
);  
INSERT INTO `posts` VALUES (1, ‘mainroot’, NULL, ‘a root-post without  
a parent, but with childs’);  
INSERT INTO `posts` VALUES (2, ‘left trunk’, 1, ‘this root has a  
parent and child’);  
INSERT INTO `posts` VALUES (3, ‘left leaf’, 2, ‘a leaf-post with  
parent, but without child’);  
INSERT INTO `posts` VALUES (4, ‘right leaf’, 1, ‘this leaf-post has a  
parent, but no child’);

===My model file post.rb contains:  
class Post \< ActiveRecord::Base  
def Post::find\_roots  
all\_posts = Post::find\_all  
all\_posts.find\_all {|post| not post.has\_parent?}  
end  
def has\_parent?  
not @parent == nil  
end  
end

The DB has only an id of the parent post, so I try to map this to a  
post-object like this:  
class Post \< ActiveRecord::Base  
attr\_reader :parent  
def initialize(\*args)  
puts “TEST”  
super(\*args)  
unless self.parent\_id == nil  
@parent = Post::get\_id(self.parent\_id)  
else  
@parent = nil  
end  
end  
end

??When I run [http://localhost:3001/Post/list](http://localhost:3001/Post/list) I get a list of posts, so  
so far so good but “TEST” is not printed, so it seems like  
Post#initialize is never called. Is this true? And how to solve this?

===Controller-file:  
class PostController \< ApplicationController  
scaffold :post  
def list  
@posts = Post::find\_roots  
puts @posts.length  
end  
end

By the way, the controller-file (above) doesn’t print the number of  
root-posts, but prints the total number of posts. This is makes sence,  
since Post#initialize above was never run…

Reading a bit more on internet about relations I find that the Rails’  
way of handling relations is more like:  
==New code:  
===DB:  
ALTER TABLE `posts` CHANGE `parent` `post_id` INT( 10 ) UNSIGNED NULL  
DEFAULT NULL  
This is a bit more cryptic, but Rails expects this name in the next  
step.  
??Is there any way to use a different fieldname here? ‘Parent’ is a  
lot more explaining then ‘post\_id’…

===New model-file:  
class Post \< ActiveRecord::Base  
belongs\_to :post  
has\_many :post  
def Post::find\_roots  
all\_posts = Post::find\_all  
all\_posts.find\_all {|post| not post.has\_parent?}  
end  
def has\_parent?  
not self.post == nil  
end  
end

??For some reason this makes the controller (described above) return  
zero. Is my Post#has\_parent? correct? A test shows that it is called  
for all elements, but the test contained is somehow wrong.

Basically: am I on the right by implementing the relation between Post  
and parent-Post like this?

All insight and tips are welcome!

Cheers,  
Paul

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [April 8, 2007, 10:29pm UTC](https://www.ruby-forum.com/t/how-to-hierarchical-model-object-object-with-relation-to-i/91420/2 "2007-04-08T22:29:23Z")

</div>

I found the acts\_as\_tree keyword on  
[http://www.railsforum.com/viewtopic.php?id=1560](http://www.railsforum.com/viewtopic.php?id=1560)  
that will probably make life a lot easier in the above scenario.

Any comment is still welcome ofcourse. 🙂  
Cheers,  
Paul
