Friday, April 2, 2010

Workshop 3 Online Taxi Booking System: MySQL and Database design

Workshop 3 Online Taxi Booking System: MySQL and Database design

Topic objectives

• Develop a database object design for an online taxi booking system (OTBS);
• Revise database techniques with Rails and SQL

• Describe how to use the MVC “push-based architecture” in the Ruby on Rails development environment

To do:
1.
Set up the MySQL tools on your computer.


The download and installation MySQL Tools was relatively straighforward. This application provides a user interface for the MySQL database, as opposed to using the command line editor for CRUD (Create, Retrieve, Update and Delete) database operations. Below is a screenshot of MySQL Tools running on my PC.


2. Rails will setup a new application directory for each of your Web application projects. Get InstantRails (Windows) or Locomotive (MacOS) running on your machine. Both packages install Ruby, Rails, a Web server or one called ‘Mongrel’ or another small Ruby Web server called ‘WEBrick’, and MySQL “inside a bubble” as I call it so that others parts of your system are not modified (Similarly ZOPE does with installing its own Web server and Python versions).

I had already downloaded InstantRails as part of Workshop 1 in this series of Workshops. This download, and subsequent install was also relatively straightforward. Below is a screenshot of InstantRails, with the Ruby command prompt running on my PC.


3. Once Rails is running you at http://localhost:3000, you need to configure database access. Connection to the database is specified in the config/database.yml file.

I started InstantRails and navigated to the command prompt using the "Create New Rails App" button under the Rails Apps console. I then changed directory at the command prompt to the "taxi" directory that was created as part of the previous workshop. Once in this directory I started the default Rails webserver (Mongrel) by entering "ruby script/server" at the command prompt. After a few seconds the server began running and I was able to navigate to http://localhost:3000 using my webbrowser. The below screen shot the resulting "Welcome Aboard" page at this URL.


Here is a screen shot of the initial default config/database.yml file contents:


Given that Ruby is automatically configured to run with SQLlite, this configuration must be update to run with MySQL. Whilst this can be amended manually, The easy way to setup the MySQL database is to use the Rails command with an override flag of “-d mysql” using the command "rails taxi -d mysql". This regenerates your whole rails project without having to make modifications. The only change is then to check the config/database.yml and make sure you have the correct password setup. The below imagine is of the database configuration after the MySQL regeneration. I have added the default password as "password".


After this is completed the rake command must be utilsied in order to create the databases specified in the config/database.yml file. This involves entering the command "rake db:create:all" within the taxi directory. This is shown in the below image.

4. Generate the Passenger model by creating the MySQL database and ‘passengers’ table from the information above.

Using the View component you can generate the scaffolding for the passengers table. This is done by entering the command "ruby script/generate scaffold passenger name:string contact_number:string suburb_origin:string street:string street_number:string building:string suburb_destination:string passenger_number:string taxi_type:string date:datetime time_required:datetime". To confirm that thi shas been successful you can view the database and table structure in MySQL. This can be seen in the image below.


5. Further work on understanding MySQL under Rails by David Mertz:

a. See “Fast-track your Web apps with Ruby on Rails” at http://www-128.ibm.com/developerworks/linux/library/l-rubyrails/


This particular site runs the student through the creation of a very basic Ruby on Rail application called Address Book. It is very easy to read and follow and is useful for a test and check and validation of the exercises completed above.

b.
The “Rolling with Ruby on Rails” series and “Cookbook recipes by Curt Hibbs and others beginning at http://www.onlamp.com/pub/a/onlamp/2005/01/20/rails.html

This is also another easy to read website which runs the student through a basic description of Ruby on Rails, walks through the installation of Ruby on Rails and My SQL and runs the student through basic code in creating a "cook book" web application.

This workshop presented something a little more practical than previous workshops. The workshop provided a hands on look at how to configure the database behind a Rails application. For someone like myself who has limited programming experience, this workshop was tough going, however I was heartened with how easy Ruby on Rails has been to learn and use.


2 comments:

  1. Hi there, I'm very impressed by how far you've got with all this. Not sure how you've managed to find the time!
    I seem to be having a lot of the same problems as you eg. not really sure what is being asked in the workshops...although you seem to be a lot more on top of it than I am!

    Do you still have the link where you downloaded InstantRails from? - I haven't been able to get this working and I don't seem to have my Ruby/RubyGems/Rails configured correctly with MySQL so thought maybe I should start again with Instant Rails if possible...
    Thanks,
    Zoe

    ReplyDelete
  2. Well-written blog. Keep blogging.
    All the best,
    Croydon Car

    ReplyDelete