Home arrow Ruby-on-Rails arrow Page 3 - Forum Implementation
RUBY-ON-RAILS

Forum Implementation


Setting up a forum is a great way to keep your customers interested and get feedback on what you're doing. This article, the first part of a four-part series, will show you how to set one up for an online bookstore using Ruby on Rails. It is excerpted from chapter six of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 3
May 20, 2010
TABLE OF CONTENTS:
  1. · Forum Implementation
  2. · Using the Threaded Forum Plugin
  3. · Setting Up the Forum
  4. · Modifying the Model

print this article
SEARCH DEVARTICLES

Forum Implementation - Setting Up the Forum
(Page 3 of 4 )

We have been using ActiveRecord migrations since we introduced them in Chapter 2. Continuing to use them for modifying the database schema to implement the Emporium forum is a good idea.

Updating the Database Schema

The forum functionality requires that we add a new table to the database schema, which we name forum_posts . This table is where all forum posts will be stored, including the information about how posts are related to each other and how they should be displayed in a hierarchical fashion.

The forum_posts table consists of ten columns, as shown in Figure 6-1. Table 6-2 provides a brief description of each column. You can use different column names and add as many columns as you need for storing your data, but we have followed the default structure.


Figure 6-1.  Forum database table

Table 6-2. Columns in the forum_posts Table 

 

Column Name

Description

root_id

The unique identifier for the root post

parent_id

The unique identifier for the parent post

lft

The left boundary of the post; replies to this post have a left boundary that is greater than this number

rgt

The right boundary of the post; replies to this post have a right boundary that is less than this number

depth

The depth of the thread

name

The name of the person who created the post

subject

The subject of the post

body

The post text

created_at

Automatically set to the date and time the record was created

updated_at

Automatically set to the date and time the record was updated

 


Tip  For in-depth information about how hierarchical data, like forum posts, can be stored in a MySQL" database, see the "Managing Hierarchical Data in MySQL article at http://dev.mysql.com/tech-resources/articles/hierarchical-data.html . This article also explains how the lft , rgt , and depth columns are used.


The first five columns--root_id , parent_id , lft , rgt , and depth are used by the plugin to maintain the hierarchy of related posts. We have added the name , subject , and body columns, which are used to store the data entered by the user.

Recall that the created_at and updated_at columns have a special meaning in ActiveRecord. If ActiveRecord finds these columns in a table, it will automatically set the column values to the current date and time, at creation time or when the record is updated.


Note  You can also name the created_at and updated_at columns created_on and updated_on ; they will be treated in the same way.


Next, create the ForumPost model, a migration, a fixture, and a unit test by executing the script/generate command:

$ script/generate model ForumPost

exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/forum_post.rb
create test/unit/forum_post_test.rb
create test/fixtures/forum_posts.yml
exists db/migrate
create db/migrate/005_create_forum_posts.rb

 

 

 

 

 

 

Add the code shown in Listing 6-1 to the migration file: db/migrate/005_create_forum_posts.rb .

Listing 6-1. ActiveRecord Migration for the Forum Table

class CreateForumPosts < ActiveRecord::Migratio n
  def self.up
   
create_table :forum_posts do |table|
      table.column :name, :string, :limit => 50, :null => false
      table.column :subject, :string, :limit => 255, :null => false
      table.column :body, :text

      table.column :root_id, :integer, :null => false, :default => 0
      table.column :parent_id, :integer, :null => false, :default => 0
      table.column :lft, :integer, :null => false, :default => 0
      table.column :rgt, :integer, :null => false, :default => 0
      table.column :depth, :integer, :null => false, :default => 0

      table.column :created_at, :timestamp, :null => false
      table.column :updated_at, :timestamp, :null => false
    end
  end

  def self.down
    drop_table :forum_posts
  end
end

When the migration script is executed, it creates the forum_posts database table and the columns shown in Figure 6-1. Remember that you should always roll back all changes done by the migration in the down method. In this case, we simply delete the forum_posts table.

Next, perform the database migration by executing rake db:migrate .

$ rake db:migrate

--------------------------------------------
(in /home/george/projects/emporium)
== CreateForumPosts: migrating ================================================ -- create_table(:forum_posts)
  
-> 0.1430s
== CreateForumPosts: migrated (0.1432s) =======================================
--------------------------------------------


EXTENDING THE FORUM

If you have different requirements for your forum, you can add as many columns to the forum_posts table as you need. For example, you might add email, first_name and last_name columns.

It is aso easy to split the forum into separate categores, such as Feedback, News, FAQ, and Help. To set up categories, add a category_id column to the forum_posts table, which references a forum category that is stored in the categories table. Then add the following code to the model:

  belongs_to :category >

  def self.find_all_in_category(category)
    category = Category.find_by_name(category)
    self.find :all, :conditions => "category_id = #{category.id}"
  end

The belongs_to mapping allows you to access the category the post belongs to; for example, post.category.name. The find_all_in_category method could be used to retrieve posts from a specific category; for example, ForumPost.find_all_in_category('FAQ').



blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials