Links | ? |
Part 1 / 3 : Sample ForumKeat | urlon 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 endIn 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
|