When I started working with Ruby and Rails in late 2004, there was almost no documentation on the Rails framework. Since then, there has been a tremendous number of books, blogs, and articles written about creating web applications with Rails. But many of them seemed to follow a common pattern: you could create a blog in 15 minutes; a to-do list application was simple. Many of the books I saw devoted an entire chapter to installing Ruby and Rails. Today, there is no lack of resources for the beginning and intermediate Rails developer.

But Rails is clearly useful for much more than toy blogs and to-do lists. The 37signals applications (Basecamp, Highrise, Backpack, and Campfire) are all built with Rails; many of the Internet’s high-traffic sites such as Twitter, Penny Arcade, and use it. Rails is now used in many high-profile places, yet developers often have to fend for themselves when building such large applications, as the most current and relevant information is often only found spread across various other developers’ blogs.

Development and deployment of complex web projects is a multidisciplinary task, and it will always remain so. In this book, I seek to weave together several different topics relevant to Rails development, from the most basic foundations of the Ruby programming language to the development of large Rails applications.


As its title suggests, Advanced Rails is not a book for beginners. Readers should have an understanding of the architecture of the Web, a good command of Ruby 1.8, and experience building web applications with Ruby on Rails. We do not cover installation of Rails, the Rails API, or the Ruby language; working-level experience with all of these is assumed.

I would recommend the following books as a prelude to this one:

  • Programming Ruby, Second Edition, by Dave Thomas (Pragmatic Bookshelf): Known as “the Pickaxe,” this is an excellent introduction to Ruby for programmers, and a comprehensive reference that will serve you for years. Without a doubt the most essential book for Rails developers, no matter what skill level.

  • The Ruby Programming Language, by David Flanagan and Yukihiro Matsumoto (O’Reilly): Scheduled to be released in January 2008, this book is a comprehensive introduction and reference to Ruby 1.8 as well as 1.9. It does an excellent job of covering even the most difficult aspects of Ruby while still being accessible to programmers learning it.

  • Best of Ruby Quiz by James Edward Gray II (Pragmatic Bookshelf): 25 selected quizzes from the Ruby Quiz (; includes both the quizzes and a discussion of their solutions. Solving programming puzzles and sharing solutions with others is a great way to hone your Ruby skills.

  • Agile Web Development with Rails, Second Edition, by Dave Thomas and David Heinemeier Hansson (Pragmatic Bookshelf): The best and most comprehensive book for learning Ruby on Rails. The second edition covers Rails 1.2, but most concepts are applicable to Rails 2.0.

  • Rails Cookbook, by Rob Orsini (O’Reilly): This contains “cookbook-style” solutions to common problems in Rails, each one of which may be worth the price of the book in time saved. Also worth reading are the similar books Rails Recipes by Chad Fowler and Advanced Rails Recipes by Mike Clark and Chad Fowler (Pragmatic Bookshelf).

Many varied subjects are covered in this book; I make an effort to introduce subjects that may be unfamiliar (such as decentralized revision control) and provide references to external resources that may be useful. Each chapter has a “Further Reading” section with references that clarify or expand on the text.

I take a bottom-up approach to the concepts in this book. The first few chapters cover the mechanics of metaprogramming in Ruby and the internals of Rails. As the book progresses, these concepts assimilate into larger concepts, and the last several chapters cover the “big-picture” concepts of managing large Rails software development projects and integrating Rails into other systems.

This book is written for Rails 2.0. At the time of this writing, Rails 2.0 has been released as a release candidate, but not in its final form. Details are subject to change, but the concepts and techniques discussed in this book should be valid for Rails 2.0.

Conventions Used in This Book

The following typographical conventions are used in this book:

Plain text

Indicates menu titles, menu options, menu buttons, keyboard accelerators (such as Alt and Ctrl), plugins, gems, and libraries.


Indicates new terms, URLs, email addresses, filenames, file extensions, pathnames, directories, controls, and Unix utilities.

Constant width

Indicates commands, options, switches, variables, attributes, keys, functions, types, classes, namespaces, methods, modules, properties, parameters, values, objects, events, event handlers, interfaces, XML tags, HTML tags, macros, the contents of files, or the output from commands.

Constant width italic

Shows text that should be replaced with user-supplied values.

Constant width bold

Used to highlight portions of code.


This icon signifies a tip, suggestion, or general note.


This icon indicates a warning or caution.

Using Code Examples

This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: "Advanced Rails, by Brad Ediger. Copyright 2008 Brad Ediger, 978-0-596-51032-9.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .

How to Contact Us

Please address comments and questions concerning this book to the publisher:

O’Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
800-998-9938 (in the United States or Canada)
707-829-0515 (international or local)
707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at:

To comment or ask technical questions about this book, send email to:

For more information about our books, conferences, Resource Centers, and the O’Reilly Network, see the web site:

Safari® Books Online

When you see a Safari® Books Online icon in your favorite technology book, that means the book is available online through Safari Books Online.

Safari offers a solution that’s better than e-books. It’s a virtual library that lets you easily search thousands of top tech books, cut and paste code samples, download chapters, and find quick answers when you need the most accurate, current information. Try it for free at


No book is created without the help of many people. I owe a great debt of gratitude to the many who helped create this work. Without their help and support, these ideas would still be rattling around in my head.

Mike Loukides, my editor at O’Reilly, was instrumental in creating the idea for this book. He helped me understand the type of book I really wanted to write, and provided the encouragement needed to turn sketches of ideas into prose. Mike’s extensive knowledge of the industry, the authorship process, and computer science in general were invaluable.

I had an amazing team of technical reviewers, who caught many of my errors in the manuscripts. Thanks are due to James Edward Gray II, Michael Koziarski, Leonard Richardson, and Zed Shaw for their revisions. Any remaining errors were originated and perpetuated on my own. (Should you find one of these errors, we’d love to hear about it at

The production department at O’Reilly was very professional and accommodating of my odd schedule; Keith Fahlgren, Rachel Monaghan, Rob Romano, Andrew Savikas, Marlowe Shaeffer, and Adam Witwer all helped make this book usable and attractive.

I have many friends and colleagues who offered advice, support, criticism, and review. Thanks to Erik Berry, Gregory Brown, Pat Eyler, James Edward Gray II, Damon Hill, Jim Kane, John Lein, Tim Morgan, Keith Nazworth, Rob Norwood, Brian Sage, Jeremy Weathers, and Craig Wilson for your input. Thanks also to Gary and Jean Atkins, who, although they know nothing about Rails or software development, never failed to ask me about my book’s progress and offer encouragement.

Others provided inspiration through their books and writings online, as well as discussions on mailing lists: François Beausoleil, David Black, Avi Bryant, Jamis Buck, Ryan Davis, Mauricio Fernández, Eric Hodel, S. Robert James, Jeremy Kemper, Rick Olson, Dave Thomas, and why the lucky stiff.

None of this would have been possible without Ruby or Rails. Thanks to Yukihiro Matsumoto (Matz) for creating such a beautiful language, to David Heinemeier Hansson for creating such a fun framework, and to the Ruby and Rails committers and communities for maintaining them.

Thanks to my parents for their continual support.

Finally, thanks to my wonderful wife, Kristen, who put up with a year-long writing process. She encouraged me to write a book when I thought it impossible, and supported me every step of the way.

Get Advanced Rails now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.