Links ?

Part 1 / 3 : Sample Forum

Keat | url
on Mon Sep 13 20:32:00 SGT 2004

This is a sample forum written as a quick intro to Ruby and Rails.

The only table used is:

CREATE TABLE posts (
  id int(19) NOT NULL auto_increment,
  subject varchar(255) NOT NULL default '',
  message text NOT NULL,
  author varchar(255) NOT NULL default '',
  email varchar(255) default NULL,
  url varchar(255) default NULL,
  created_at datetime default NULL,
  post_id int(19) default NULL,
  type varchar(255) default NULL,
  PRIMARY KEY  (id)
)
Using ActiveRecord class in Rails, id, post_id and type columns of the table is dealt with automatically. The main model (in MVC) here is Post. And in rails, you do this (the 'def before_destroy' chunk is quite optional)
require 'active_record'
require 'forum_controller'

class Post < ActiveRecord::Base
  def before_destroy
    sqlId = ForumController.quote(id)
    Comment.find_all("post_id = #{sqlId}").each do |entry|
      entry.destroy
    end
  end
end
In our forum, we'll have comments on the topics posted. Declaring this model is even easier:
require 'post'

class Comment < Post
  belongs_to :post
  # it is assumed the foreign_key => "post_id"
  # however, manually assigning a foreign key
  # will work also too.
end

? ?

Comments (2)

? Keith, yes, there is. As long as we're talking about the id field of an object, you don't have to name it explicitly to find().
| url
on Mon Sep 20 18:46:00 SGT 2004 [Delete]

?

? I'm not learned enough about Ruby or Rails .. but hopefully there is actually a framework/method to do
Comment.find_all("post_id = #{sqlId}").each ...
without exposing the column name.
Keat
on Mon Sep 13 21:21:00 SGT 2004 [Delete]

?

<< Back | Comment | Edit | Delete