Working with Ruby
Now that ruby and mysql appear to be working ok, I need to set up a new database. First of all, look at the config/database.yml file. The root user password (or whichever mysql user that you’re going to use) must be put in this file. Then, the database must be created:
yo:~/work/ruby/test1/config maryh$ mysqladmin -u root -p create test1_development Enter password: yo:~/work/ruby/test1/config maryh$ mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 249 to server version: 5.0.27-standard Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | | test1_development | +--------------------+ 4 rows in set (0.00 sec)
Looks good. Our database has been created.
Next:
script/generate migration accounts
vi db/migrate/001_accounts.rb
class Accounts < ActiveRecord::Migration
def self.up
create_table :accounts do |table|
table.column :name, :string
table.column :budget, :float
end
end
def self.down
drop_table :accounts
end
end
yo:~/work/ruby/test1 maryh$ rake migrate
(in /Users/maryh/work/ruby/test1)
== Accounts: migrating ========================================================
-- create_table(:accounts)
-> 0.0029s
== Accounts: migrated (0.0030s) ===============================================
The rake task migrate has been deprecated, please use the replacement version db:migrate
So far, so good. I don’t really know what I’m doing, but whatever it is, it’s working.
At this point, I have a database and a schema set up for storing accounts. Now I need to start creating accounts and in rails, this is done with scaffolding.
yo:~/work/ruby/test1 maryh$ script/generate scaffold account expenses
Now restart the server and see how the what I just did, looks.
localhost:3000/expenses
Ok, things seem to work. I was following through the example here: