Creating the comments database tables and controller is done much in the same way the posts database tables and controller was created--by using the scaffold generator. The scaffold generator will create RESTful controllers, map routes and create database migrations. But before you take this on, you have to think about what a comment is and what its data members will be. A comment has:
- Name (required field): The name of the commenter as a string.
- Email (optional field):The email of the commenter as a string.
- Body (required field): The body of the comment as text.
- post: This associates the comment with a particular blog post. This is required for the has_many and belongs_to associations.
One you've decided what a comment's data members are, you can run the scaffold generator. Note that the post field is of the type "references." This is a special type that will generate an ID field to link the comments table with the posts table via a foreign key.
$ script/generate scaffold comment name:string email:string body:text post:references
exists app/models/
exists app/controllers/
exists app/helpers/
... snip ...
Once the controllers and migrations are generated, you can go ahead and run the migration by running the db:migrate rake task.
$ rake db:migrate
== 20080724173258 CreateComments: migrating ========
-- create_table(:comments)
-> 0.0255s
== 20080724173258 CreateComments: migrated (0.0305s)

