Home arrow Ruby-on-Rails arrow Writing a Ruby-on-Rails Bookstore Application

Writing a Ruby-on-Rails Bookstore Application

In this second part to a three-part series on doing the project setup and proof of concept for a fictional bookstore on Ruby-on-Rails, we'll finish adding the software and start building the application itself. This article is excerpted from chapter one of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 6
April 29, 2010
  1. · Writing a Ruby-on-Rails Bookstore Application
  2. · Introducing Scrum
  3. · Creating the Emporium Application
  4. · Creating the Emporium Database

print this article

Writing a Ruby-on-Rails Bookstore Application
(Page 1 of 4 )

Installing MySQL

Next, we ask George if he has a database we can use for storing the authors, books, and orders. He replies, “Sure, I have a database. Follow me to the office and I’ll show you.” To our horror, he fires up Microsoft Excel and proceeds to show us the orders from the previous eight years, all stored in a single spreadsheet. We try to keep a straight face and tell him that Ruby on Rails doesn’t support spreadsheets, but it currently supports the following databases:

  1. Oracle
  2. IBM DB2 
  3. MySQL 
  4. PostgreSQL 
  5. SQLite 
  6. Microsoft SQL Server 
  7. Firebird

Each of these databases has its own strengths and weaknesses. MySQL, which is what we will use in this book, is a good choice if you are looking for a fast and easy-to-use database.

MySQL is an open source database server that is developed and owned by MySQL AB, a Swedish company founded by David Axmark and Michael “Monty” Widenius. MySQL is used by many high-traffic websites, includingcraigslist.comanddigg.com.

In this book, we use MySQL version 5, which supports advanced features like clustering, stored procedures, and triggers. This means that all of the examples and code have been tested with this version, and they might not work with earlier versions. You can either use a precompiled package or compile from source.

Go to the MySQL homepage atwww.mysql.comand click the Developer Zone tab. Click Downloads and find and download the latest stable binary release of MySQL.

Tip  You can also useapt-geton Ubuntu (sudo apt-get install mysql-server) to download MySQL, but you are not guaranteed to get the latest version.

We downloaded and installed the Linux binary package (not the RPM file that’s offered for download), and then extracted the contents of the package (replace the filename with the name of the file you downloaded):

$ tar zxvf mysql-standard-version.tar.gz

To complete the installation, follow the instructions in theINSTALL-BINARYfile, located in the root of the source directory.

Tip  If you are new to MySQL, we highly recommend that you also install MySQL Query Browser and MySQL Administrator, which both can be downloaded from the MySQL Developer Zone page. MySQL Administrator, as its name implies, can be used for managing your MySQL sever. MySQL Query Browser allows you to run SQL queries and scripts from a graphical user interface. Both applications are available for Linux, Windows, and Mac OS X. If you’re using OS X, a good application for both managing your databases and executing queries is CocoaMySQL (www.theonline.org/cocoamysql/).

Open a console window and execute the following command to start MySQL:

$ mysqld_safe --user=mysql &

The command starts the MySQL server in the background using themysqluser. Verify that you can connect to the database with the following command:

$ mysql -u root

You should see the following:

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2 to server version: 5.0.19-standard

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.


Installing the MySQL Driver

Ruby on Rails needs a database driver to communicate with the MySQL server. Ruby on Rails comes with a pure Ruby MySQL driver, but we want to use the native C driver written by Tomita Masahiro, as it is considerably faster.

Note  Each database requires a different driver, since there is no standard protocol. For more information about which databases are supported and how to get more information, refer to the Ruby on Rails wiki page on database drivers:http://wiki.rubyonrails.com/rails/pages/DatabaseDrivers.

First, you must install the MySQL development library before installing the native MySQL driver. On Ubuntu, you can find out which versions of the library are available by executingapt-cache search libmysqlclient(note that your system might have different versions of the library):

$ apt-cache search libmysqlclient

libmysqlclient10 - LGPL-licensed client library for MySQL databases libmysqlclient10-dev - LGPL-licensed client development files for MySQL databases libmysqlclient12 - mysql database client library
libmysqlclient12-dev - mysql database development files
libqt4-sql - Qt 4 SQL database module libmysqlclient14 - mysql database client library
libmysqlclient14-dev - mysql database development files

Next, install the correct version withapt-get. Recall that we are using MySQL 5:

$ sudo apt-get install libmysqlclient14-dev

Note  On Ubuntu, you should installlibmysqlclient14-devfor MySQL 5, andlibmysqlclient12devfor MySQL 4.

On Ubuntu, you should install for MySQL 5, and for MySQL 4.

Next, install the MySQL driver with the RubyGemsinstallcommand:

$ sudo gem install mysql

Once again, RubyGems goes out on the Internet and downloads and installs the latest version of the MySQL driver. If everything goes well, you should see the following success message in the console:

Successfully installed mysql-2.7

If you get an error message, you probably forgot to install the MySQL development libraries or some other dependency.

blog comments powered by Disqus

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials