Forum: Ruby on Rails How-to: Hierarchical model object: Object with relation to i

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
66f87cbe899dad04c4bb30b3e96434e2?d=identicon&s=25 (Guest)
on 2007-04-08 20:38
(Received via mailing list)
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!

66f87cbe899dad04c4bb30b3e96434e2?d=identicon&s=25 (Guest)
on 2007-04-08 22:29
(Received via mailing list)
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. :)
This topic is locked and can not be replied to.