20070920 Thursday September 20, 2007

Life on Rails

I have heard enough of rails. I wanted to create a simple rails application that holds some flight information. I'm going to just focus on the getting started information for rails on Freespire.

I'm trying Freespire (built on Ubuntu 7.04). Getting Ruby/Rails package and installing them is a breeze:
$ sudo apt-get install ruby rubygems irb ri rdoc ruby1.8-dev build-essential
$ sudo gem install rails --include-dependencies

$ ruby -v

ruby 1.8.5 (2006-08-25) [i486-linux]

$ sudo apt-get install libmysql-ruby mysql-server$ sudo apt-get install libopenssl-ruby
I'm going to use MySQL server for my simple flights application. I already have MySQL Server 5.0 installed. So I'm going to start the server and create a test database:
$ sudo mysqld_safe --skip-grant-tables
Starting mysqld daemon with databases from /var/lib/mysql
mysqld_safe[8850]: started
...
$ mysql
mysql> create database flights;
Query OK, 1 row affected (0.00 sec)
I created rails environment:
$ rails flights
      create  app/controllers
      create  app/helpers
      create  app/models
      create  app/views/layouts
      create  config/environments
....
I provided DB Information in config/database.yml. Here is the snippet:
development:
  adapter: mysql
  database: flights
  username:
  password:
  host: localhost
socket: /var/run/mysqld/mysqld.sock
Since I have started the server bypassing the grant tables, I don't have to provide user information here. Change the mysqld.sock path to suit your settings.

I can create tables in MySQL through a rake migration script. That way my app. is not tightly coupled with any server and I can migrate my schema to another production server later.

I created the skeleton files for our flights table.
$ script/generate migration flights
      create  db/migrate
      create  db/migrate/001_flights.rb
I modified my db/migrate/001_flights.rb file with the flights schema information:
class Flights < ActiveRecord::Migration
  def self.up
    create_table :flights do |table|
        table.column :name, :string
        table.column :from, :string
        table.column :to, :string
        table.column :duration, :int
        table.column :stops, :int
    end
  end
  def self.down
    drop_table :flights
  end
end
OK now I have my flights schema ready. I need to invoke the rake migrate script to update the flights DB.
$ rake db:migrate
(in /home/phantom/rails_sample/flights)
== Flights: migrating =========================================================
-- create_table(:flights)
   -> 0.0389s
== Flights: migrated (0.0395s) ================================================
I checked my DB:
mysql> use flights;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+-------------------+
| Tables_in_flights |
+-------------------+
| flights           |
| schema_info       |
+-------------------+
2 rows in set (0.00 sec)

mysql> desc flights;
+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| id       | int(11)      | NO   | PRI | NULL    | auto_increment |
| name     | varchar(255) | YES  |     | NULL    |                |
| from     | varchar(255) | YES  |     | NULL    |                |
| to       | varchar(255) | YES  |     | NULL    |                |
| duration | int(11)      | YES  |     | NULL    |                |
| stops    | int(11)      | YES  |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
There I go. The table created by rails is ready.
Based on the schema created, I'm going to generate my scaffolding code.
$ script/generate scaffold flight flights
      exists  app/controllers/
      exists  app/helpers/
      create  app/views/flights
      exists  app/views/layouts/
      exists  test/functional/
  dependency  model
      exists    app/models/
      exists    test/unit/
      exists    test/fixtures/
      create    app/models/flight.rb
      create    test/unit/flight_test.rb
      create    test/fixtures/flights.yml
...

where flight is my model name and flights is my controller name. Sometimes the singular/plural restrictions of rails get on my skull.

I'm going to test my application:
$ script/server
=> Booting WEBrick...
=> Rails application started on http://127.0.0.1:3000
=> Ctrl-C to shutdown server; call with --help for options
[2007-09-20 13:24:30] INFO  WEBrick 1.3.1
[2007-09-20 13:24:30] INFO  ruby 1.8.5 (2006-08-25) [i486-linux]
[2007-09-20 13:24:30] INFO  WEBrick::HTTPServer#start: pid=9062 port=3000
With some records in the table my application looks like:



Do you know you can do all this in a more intuitive way using Netbeans 6.0? Netbeans 6.0 has a very good support for Ruby and Rails. Check this out.



( Sep 20 2007, 01:28:32 AM PDT ) Permalink Comments [1]
Comments:

The definitive guide for a newbie first RoR application... like me :P

Good job!

Posted by Victor Saldaña D. on September 23, 2007 at 04:58 PM PDT #

Post a Comment:

Comments are closed for this entry.