2. The database
2.1 Creating the table
Let’s assume that you have successfully installed Rails, we’ll now get into the meat of the subject, how to build your TODO list program.
The first thing we’ll think about is the database model. Since this is an introductory tutorial, we’ll keep the thing really simple, and have a single table with only three (3) columns: id, description, done. Since I’m really lazy and I don’t like creating SQL tables and databases by hand, I use phpMyAdmin (OSX users can use the excellent CocoaMySQL), but I will give here the SQL query to create our database and table:
-- Create the database CREATE DATABASE `todo` ; -- Create the table CREATE TABLE `todos` ( `id` INT NOT NULL AUTO_INCREMENT , `description` VARCHAR( 100 ) NOT NULL , `done` TINYINT DEFAULT '0' NOT NULL , PRIMARY KEY ( `id` ) );
So, every todo item will have a unique id (by the way, with Rails it is strongly suggested that you always use ‘id’ as your primary key in a table, helps a whole lot) a description and a done flag that indicates whether an item has been done or not.