How-to: Hierarchical model object: Object with relation to it's own class

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?}
  def has_parent?
    not @parent == nil

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"
    unless self.parent_id == nil
      @parent = Post::get_id(self.parent_id)
      @parent = nil

??When I run 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?

class PostController < ApplicationController
  scaffold :post
  def list
    @posts = Post::find_roots
    puts @posts.length

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:
ALTER TABLE `posts` CHANGE `parent` `post_id` INT( 10 ) UNSIGNED NULL
This is a bit more cryptic, but Rails expects this name in the next
??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?}
  def has_parent?
    not == nil

??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!


I found the acts_as_tree keyword on
that will probably make life a lot easier in the above scenario.

Any comment is still welcome ofcourse. :slight_smile: