Working with Ruby
To set up a new project: ruby cookbook (using cookbook as the project name)
This will create the entire directory structure for a ruby project. Inside the directory called cookbook is:
-rw-r--r-- 1 maryh maryh 8001 Feb 1 11:10 README -rw-r--r-- 1 maryh maryh 307 Feb 1 11:10 Rakefile drwxr-xr-x 6 maryh maryh 204 Feb 1 11:10 app drwxr-xr-x 2 maryh maryh 68 Feb 1 11:10 components drwxr-xr-x 7 maryh maryh 238 Feb 1 11:10 config drwxr-xr-x 2 maryh maryh 68 Feb 1 11:10 db drwxr-xr-x 3 maryh maryh 102 Feb 1 11:10 doc drwxr-xr-x 3 maryh maryh 102 Feb 1 11:10 lib drwxr-xr-x 6 maryh maryh 204 Feb 1 11:10 log drwxr-xr-x 14 maryh maryh 476 Feb 1 11:10 public drwxr-xr-x 12 maryh maryh 408 Feb 1 11:10 script drwxr-xr-x 8 maryh maryh 272 Feb 1 11:10 test drwxr-xr-x 6 maryh maryh 204 Feb 1 11:10 tmp drwxr-xr-x 3 maryh maryh 102 Feb 1 11:10 vendor
Once you have this, edit config/database.yml with the info for your (in my case) mysql database.
Next, create the new database:
yo:~/work/ruby/cookbook maryh$ mysqladmin -u root -p create cookbook_development Enter password:
Here’s where I get confused. I need to create tables in this new database. One example has me basically going into mysql and manually making them, while another has me use the migrate command.
script/generate migration recipes
This creates the db/migrate/001_recipes.rb file. Inside this file is where I believe I set up the tables for my database.
class Recipes < ActiveRecord::Migration def self.up create_table :recipes do |table| <--------I added this line table.column :title, :string <--------I added this line table.column :instructions, :text <--------I added this line end <-------I added this line end def self.down drop_table :recipes <-------I added this line end end
Now if I run rake migrate, it should create this table with these fields.
Looks good!
yo:~/work/ruby/cookbook maryh$ mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 9 to server version: 5.0.27-standard Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> show tables; +--------------------------------+ | Tables_in_cookbook_development | +--------------------------------+ | recipes | | schema_info | +--------------------------------+ 2 rows in set (0.00 sec) mysql> describe recipes; +--------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | title | varchar(255) | YES | | NULL | | | instructions | text | YES | | NULL | | +--------------+--------------+------+-----+---------+----------------+ 3 rows in set (0.03 sec)