Table Of ContentRuby on Rails Enterprise
Application Development
Plan, Program, Extend
Building a complete Ruby on Rails business application
from start to finish
Elliot Smith
Rob Nichols
BIRMINGHAM - MUMBAI
Ruby on Rails Enterprise Application Development
Plan, Program, Extend
Copyright © 2007 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval
system, or transmitted in any form or by any means, without the prior written
permission of the publisher, except in the case of brief quotations embedded in
critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of
the information presented. However, the information contained in this book is sold
without warranty, either express or implied. Neither the authors, Packt Publishing,
nor its dealers or distributors will be held liable for any damages caused or alleged to
be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all the
companies and products mentioned in this book by the appropriate use of capitals.
However, Packt Publishing cannot guarantee the accuracy of this information.
First published: November 2007
Production Reference: 1011107
Published by Packt Publishing Ltd.
32 Lincoln Road
Olton
Birmingham, B27 6PA, UK.
ISBN 978-1-847190-85-7
www.packtpub.com
Cover Image by Rob Nichols
Credits
Authors Project Manager
Elliot Smith Abhijeet Deobhakta
Rob Nichols
Project Coordinator
Reviewer Sagara Naik
Keynan
Indexer
Senior Acquisition Editor Monica Ajmera
David Barnes
Proofreader
Development Editor Cathy Cumberlidge
Mithil Kulkarni Chris Smith
Technical Editor Production Coordinator
Swapna V. Verlekar Shantanu Zagade
Editorial Manager Cover Designer
Dipali Chittar Shantanu Zagade
About the Authors
Elliot Smith has worked in IT since 1996: at OpenAdvantage (an open-source
solutions center) as a business analyst, as a learning technologist and web developer
at the University of Birmingham, England, and as a technical writer for Armada
Computer Publications. He runs his own training and consulting company, mooch
labs, when he gets a chance. He has an M.Sc. in Artificial Intelligence and a Ph.D. in
Computer Science from the University of Birmingham.
He thanks Nicola, his wife, for giving him the time and space to
write a book; Madeleine, his daughter, for keeping him sane while
doing it; and Rob Nichols for giving him the opportunity in the
first place.
Rob Nichols first started using computers during his apprenticeship at Rolls-Royce
in the early 1980s. At 23, he decided to change direction and started a degree in
Geology and Geography at Cardiff University. By 1995 he had gained a Ph.D. from
Bristol University, studying the behavior of quicksand.
During his time in Bristol and in a subsequent lectureship at Leeds University,
he started using the fledgling Internet to communicate with co-workers, gather
information, and present Geological information in the form of his first web pages.
Following his return to Britain from a lectureship in U.S.P. Fiji, Rob found himself
without another lectureship position to go on to. So, changing direction again, he
started working for a U.K. computer manufacturer, where he rose to the position of
Engineering Manager, managing a team of seventy maintenance and networking
engineers, and support staff.
Following the collapse of the U.K. computer market in 2002 he moved on to the
role of IT manager for a small business providing products and services to the
water industry. In this role, Rob has had great success developing intranet-based
business applications that streamlined business processes, saved time, and increased
efficiency. In doing so, he transformed the IT department from a business cost to a
profit generator by reducing costs and thereby increasing margins.
When not working with computers, Rob and his wife reside happily in a small
Midlands town in England, where he writes scripts for the local movie-makers club
and photographs the local wildlife.
Thank-you Diane, for putting up with my disappearances into the
study to "work on the book".
Table of Contents
Preface 1
Chapter 1: Introduction 7
Why this Book? 7
Why Develop? 8
Why a Client/Server Based Web Application? 9
But why Ruby on Rails? 11
Rails Handles Menial Tasks 11
Clear Code 12
Text Based File 13
Open Source 14
Plentiful Documentation 14
Built-in Safe Test Environment 15
Ruby on Rails in Detail 15
Summary 16
Chapter 2: The Initial Problem 17
A Normal Day in the Office 17
Examining the Data 19
Data Objects 20
Database Table Design Rules 21
Separating the Data 22
Naming Conventions 22
Use Meaningful Names 22
Use a Consistent Naming Convention 23
Ruby on Rails Naming Conventions 24
Constants and Classes 24
Variables 24
Methods and Properties 24
Special Method and Property Suffixes 25
Reserved Words 26
Table of Contents
Back to the Data 26
Review the Result 29
Project Preparation Steps 30
How Good is the Source Data? 30
Tracking Who does What 32
No Log-On and No Authentication 33
Simple Password Access 34
User Log-On 35
Recording Access History 35
Access Control for Rory's Application 36
Data Validation 37
The Minimum Required Data is Entered 37
Each Record can be Uniquely Identified 38
Identify Fields that Need to Have a Particular Format 39
References to Data in Other Tables Point to Actual Data 41
Rory's Data 41
Person 42
Company 42
Address 42
Summary 42
Chapter 3: Laying the Foundations 43
Supporting Rails Development 43
Addressing the Challenges 45
Setting Up a Rails Stack 46
Installing a Rails Stack Using a Bundle 48
Installing a Custom Rails Stack 49
Installing Ruby and Rubygems 49
Ruby on Windows 50
Ruby on Linux 51
Ruby on Mac OS X 52
Installing Rails 52
A Note on Rails Documentation 53
Other Libraries 55
Capistrano for Easier Deployment 55
Mongrel: A Better Way to Run Rails Applications 56
Choosing a Database Platform 57
Installing MySQL 58
Checking Your MySQL Installation 61
MySQL GUI Tools 62
Ruby-MySQL: Making Ruby and MySQL Work Better Together 62
Installing an IDE 64
Eclipse 65
EasyEclipse 67
[ ii ]
Table of Contents
Instructions for Masochists 71
In the Back Rooms at Acme… 71
Setting Up a Team Server 72
Quick Gem Installation 73
Remote Access via SSH 74
Adding Users 74
Version Control with Subversion 75
Subversion Standard Practices 77
Setting Up a Subversion Repository 81
Setting Up a Project in Subversion 82
Browsing Subversion from Eclipse 83
Other Subversion Clients 88
Using Other People's Servers 88
Back at Acme 88
Summary 89
Chapter 4: Working with Rails 91
The World According to Rails 92
Model-View-Controller Architecture 92
Convention over Configuration 94
Rails and MVC 95
Setting Up a New Rails Application 97
Using Mongrel to Serve Your Application 100
Connecting Rails to a Database 103
Creating a Database and System Account 104
Setting Up a Database Connection 106
Configuring the Rails Environments 108
Testing the Database Connection 111
Troubleshooting a MySQL Connection 112
ActiveRecord, Migrations, and Models 113
Model == Table 114
Which Comes First: The Model or The Table? 114
Building a Model with Migrations 115
Converting a Data Structure into a Migration 117
Defining Columns in Migrations 119
Other Operations Available in a Migration 121
Running a Migration 122
Rolling Back to a Previous Version of the Database 124
The Scaffold 125
Completing the Database 127
The companies Table 127
The addresses Table 127
Generating the Remaining Tables 128
Models in Detail 129
[ iii ]