1. Links
2. Notes
Day 1 Lines:
-
Database schema initialization: 16 lines
-
Shell commands: 4 lines
-
Ruby code: 3 line
rails ToDo cd ToDo dir="$(pwd)" # warning: you may need to add this line to script/server: # require 'sqlite'
Setup databse:
-
MySQL... which I won't be using =P (i.e. ignore this: it's only for reference)
CREATE TABLE `categories` ( `id` smallint(5) unsigned NOT NULL auto_increment, `category` varchar(20) NOT NULL default '', `created_on` timestamp(14) NOT NULL, -- ActiveRecord::Timestamp `updated_on` timestamp(14) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `category_key` (`category`) ) TYPE=MyISAM COMMENT='List of categories';
-
SQLite
cat > config/database.yml << EOF development: adapter: sqlite dbfile: db/rails-development.db test: adapter: sqlite dbfile: db/rails-test.db production: adapter: sqlite dbfile: db/rails-production.db EOF for f in db/rails-{development,test,production}.db; do sqlite $f << EOF CREATE TABLE categories ( id INTEGER AUTOINCREMENT NOT NULL, category varchar(20) UNIQUE NOT NULL default '', created_on timestamp(14) NOT NULL, -- ActiveRecord::Timestamp updated_on timestamp(14) NOT NULL, PRIMARY KEY (id) ); EOF done
Data model:
-
ruby script/generate model category # exists app/models/ # exists test/unit/ # exists test/fixtures/ # create app/models/category.rb # create test/unit/category_test.rb # create test/fixtures/categories.yml
Scaffold (controller?):
-
ruby script/generate controller category # exists app/controllers/ # exists app/helpers/ # create app/views/category # exists test/functional/ # create app/controllers/category_controller.rb # create test/functional/category_controller_test.rb # create app/helpers/category_helper.rb
-
Test it:
cat > app/controllers/category_controller.rb << EOF class CategoryController < ApplicationController scaffold :category end EOF ruby script/server # connect to http://mingo:3000/category -
Add validation rules
cat > app/models/category.rb << EOF class Category < ActiveRecord::Base # ActiveRecord::Validations::ClassMethods validates_length_of :category, :within => 1..20 validates_uniqueness_of :category, :message => "already exists" end EOF
Day 2 follows... (not finished)
-
ruby script/generate scaffold category
3. Problems
"uninitialized constant SQLite" ?
-
Add this line to script/server
require 'sqlite'
