Creating the recipes Table

Our cookbook will contain recipes, so let's create a table in our database
to hold them.

In the left-hand pane of MySQL-Front, right click on the cookbook database
you just created and select New>Table... from the pop-up
menu (Figure 21).

Figure 21. Creating a new table

Name the table recipes (Figure 22).

Figure 22. The Add Table dialog box

Important note: MySQL-Front will automatically
create a primary key named Id, but Rails prefers to call it
id (all lowercase). I'll explain more later, but for now just
change it. In the left pane, select the recipes table you just
created. In the right pane, right-click on the Id field, select
Properties (Figure 23), and change the name to id.

Figure 23. Renaming the primary key

Adding Recipe Fields

Now that we have a recipes table, we can start adding fields (columns) to
hold recipe data. Let's start by creating title and
instructions fields. Eventually, we will need more fields, but
this is a good place to start.

With the recipe table selected, right click in a blank area of the
right pane and select New>Field... (Figure 24).

Figure 24. Adding a new field

Create the recipe title field as a varchar(255) with nulls
not allowed, so that every recipe must have title text. Figure
25 shows the options in the pop-up window.

Figure 25. Adding the title field

Repeat the above procedure to create an instructions field as
text, as shown in Figure 26.

Figure 26. Adding the instructions field

The recipe table should now resemble Figure 27.

Figure 27. The modified recipe table

The Excitement Begins

Everything we have done up to this point has been pretty short and painless,
but not particularly exciting. This is where that changes. We can now have the
very beginnings of our cookbook application up and running in record
time!

Create the Model

First, create a Recipe model class that will hold data from the
recipes table in the database. Figure 28 shows where it should
live.

Figure 28. The Recipe model class

Open a command window to the cookbook directory (c:\rails\cookbook)
and run the command:

ruby script\generate model Recipe

This will create a file named recipe.rb containing a skeleton
definition for the Recipe class. Right-click on this file and
choose Edit to look inside (Figure 29).

Figure 29. The contents of recipe.rb

This seemingly empty class definition is the recipe business object that
Rails maps to the recipes table in the database. You will see more
concretely what I mean by this in a moment. Right now, I want to point out that
this little bit of programming magic happened because we used a Rails naming
convention: a singular model class name (Recipe) maps to a plural
database table (recipes). Rails is smart about English
pluralization rules, so Company maps to companies, Person maps to
people, and so forth.

Further, Rails dynamically populates the Recipe class with
methods for accessing the rows in the recipes table and an
attribute for each column in the table.

Very shortly, you will see a dramatic demonstration of this dynamic
connection between the Recipe class and the recipes table.

We are now very close to seeing something work. We need to create a recipe
controller (Figure 30) with actions to manipulate the recipes in the database
via the standard CRUD operations: create, read, update, and delete. Rails makes
this easier than you might think.

Figure 30. The Recipe controller in its
native environment

Open a command window in the cookbook directory (c:\rails\cookbook)
and run the command:

ruby script\generate controller Recipe

This will create a file named recipe_controller.rb containing a
skeleton definition for the RecipeController class. Right-click
on this file, choose Edit, and add the line scaffold :recipe as
shown in Figure 31.

Figure 31. One line of code in
RecipeController

This single line of code will bring the database table to life. It defines
actions for all CRUD operations, immediately allowing us to create, read,
update, and delete recipes in our database!