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).
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
The unique identifier for the root post
The unique identifier for the parent post
The left boundary of the post; replies to this post have a left boundary that is greater than this number
The right boundary of the post; replies to this post have a right boundary that is less than this number
The depth of the thread
The name of the person who created the post
The subject of the post
The post text
Automatically set to the date and time the record was created
Automatically set to the date and time the record was updated
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
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
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 .
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:
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').