CFWheels — Adding a new table with model, view, and controller
In Part 2, we looked at Editing the View in CFWheels.
We thought we were done, but now the boss wants to record events along with ticket sales. Remember we are following along in Head First Rails, and converting everything to CFWheels!
Fortunately, with scaffolding, all we need to do is create the new database table and run the scaffolding plugin.
Here’s the create table SQL query:
CREATE TABLE `events` (
`id` int(11) NOT NULL auto_increment,
`artist` varchar(255) NOT NULL,
`description` text,
`price_low` decimal NOT NULL,
`price_high` decimal NOT NULL,
`event_date` date NOT NULL,
PRIMARY KEY (`id`)
)
I got the idea from a Rails app to create a “spec” directory to hold application specification information. And, I created a database sub-directory to hold all my table creation SQL. So the query is stored in /spec/database/events.sql.
Next, click the Scaffold link. Look in the debug information at the bottom of each page, next to Plugins. Then, on the scaffold page, enter event as the Object name, and click Generate. Remember, the table is named events (plural with an “s”), but the the object is named event (singular).
All that’s left is to open /views/events/ and edit the labels in edit.cfm, index.cfm, new.cfm and show.cfm. This last step isn’t really needed to make things work, but the boss is kind of picky.
Great — We’ve finished with Chapter 1 of Head First Rails. You now know how to:
- Create a new CFWheels application
- Install the scaffolding plugin to CFWheels
- Setup a database table and use scaffolding to automatically generate the Model, View, and Controller code.
- Edit HTML in the View
- Add fields and new tables to the database
Next time, we’ll start Chapter 2, and build an application from scratch — Without using scaffolding.

I like the idea about the specs folder.. would love for Wheels to output the SQL file for me… maybe I will write a plugin for that or someone will
You know, I don’t like the way Rails does things because you end up having to learn a new SQL-like syntax. As long as you have to do that, you may as well just learn enough SQL to do things!
It would be cool to have a more visual way to specify your fields and field types that was right there inside CFWheels!
On the specs folder, I keep looking for a good way to specify and document my “has many” and “belongs to” associations. Maybe you have a good idea?