by Steve Klabnik
A long time ago, I was an intern at a technology company. We had “deploy week,” meaning that after deploying, we took an entire week to fight fires. Moving our code to the production environment would inevitably cause unexpected changes. One day, I read a blog post titled “Unit Testing with Ruby on Rails,” and my life was forever changed. I excitedly went and told my team that we could write code to check whether our code worked before deploying, but they weren’t particularly interested. A few months later, when a friend asked me to be the CTO of his startup, I said, “Only if I can do it in Ruby on Rails.”
My story was fairly typical for that period. I didn’t know anything about Ruby, but I had to write my application in Rails. I figured out enough Ruby to fake it and cobbled together an application in record time. There was just one problem: I didn’t really understand how it actually worked. This is the deal everyone makes with Rails at the start. You can’t think about the details too much because you’re flying to the sky like a rocket.
This book, however, isn’t about that. When I read The Rails Way for the first time, I felt like I truly understood Rails for the first time. All those details I didn’t fully understand were now able to be grokked. Every time someone said, “Rails is magic,” I would smile to myself. If Rails was magic, I had peered behind the curtain. One day, I decided that I should write some documentation to help dispel those kinds of comments. One commit became two; two became twenty. Eventually, I was a large contributor in my own right. Such a long way for someone who had just a few short years earlier never heard of a unit test!
As Rails has changed, so has The Rails Way. In fact, one criticism you could make of this book is that it’s not actually “the Rails way”; after all, it teaches you HAML instead of ERb! I think that this criticism misses the mark. After all, it’s not 2005 anymore. To see what I mean, go read the two forewords from the previous edition. They appear right after this one ... I’ll wait.
Done? David’s foreword was quite accurate for both Rails 2 and The Rails Way. At that time, Rails was very much “not as a blank slate equally tolerant of every kind of expression.” Rails was built for what I call the “Omakase Stack”: you have no choice, you get exactly what Chef David wants to serve you.1
Yehuda’s foreword was also quite accurate—but for Rails 3 and The Rails™ 3 Way. “We brought this philosophy to every area of Rails 3: flexibility without compromise.” With Rails 3, you get the Omakase stack by default, but you are free to swap out components: if you don’t like sushi, you can substitute some sashimi.
There was a lot of wailing and gnashing of teeth during the development of Rails 3. Jeremy Ashkenas called it “by far the worst misfortune to ever happen to Rails.” Rails 3 was an investment in the future of Rails, and investments can take a while to pay off. At the release of Rails 3, it seemed like we had waited more than a year for no new features. Rails was a little better but mostly the same. The real benefit was where it couldn’t be seen: in the refactoring work. Rails 1 was “red-green.” Rails 2 was “red-green.” Rails 3 was “refactor.” It took a little while for gem authors to take advantage of this flexibility, but eventually, they did.
And that brings us to Rails 4 and The Rails™ 4 Way. This book still explains quite a bit about how Rails works at a low level but also gives you an alternate vision from the Omakase Stack, based on the experience and talent of Hashrocket. In many ways, The Rails™ 4 Way, Agile Web Development with Rails, and Rails 4 in Action are all “the Rails way.” Contemporary Rails developers get the best of both worlds: They can take advantage of the rapid development of convention over configuration, but if they choose to follow a different convention, they can. And we have many sets of conventions to choose from. It’s no longer “David’s way or the highway,” though David’s way is obviously the default, as it should be.
It has been an amazing few years for Rails, and it has been a pleasure to take a part in its development. I hope that this book will give you the same level of insight and clarity into Rails as it did for me, years ago, while also sparking your imagination for what Rails will undoubtedly become in the future.
Foreword to the Previous Edition
by David Heinemeier Hansson
Rails is more than programming framework for creating web applications. It’s also a framework for thinking about web applications. It ships not as a blank slate equally tolerant of every kind of expression. On the contrary, it trades that flexibility for the convenience of “what most people need most of the time to do most things.” It’s a designer straightjacket that sets you free from focusing on the things that just don’t matter and focuses your attention on the stuff that does.
To be able to accept that trade, you need to understand not just how to do something in Rails but also why it’s done like that. Only by understanding the why will you be able to consistently work with the framework instead of against it. It doesn’t mean that you’ll always have to agree with a certain choice, but you will need to agree to the overachieving principle of conventions. You have to learn to relax and let go of your attachment to personal idiosyncrasies when the productivity rewards are right.
This book can help you do just that. Not only does it serve as a guide in your exploration of the features in Rails, but it also gives you a window into the mind and soul of Rails. Why we’ve chosen to do things the way we do them and why we frown on certain widespread approaches. It even goes so far as to include the discussions and stories of how we got there—straight from the community participants that helped shape them.
Learning how to do Hello World in Rails has always been easy to do on your own, but getting to know and appreciate the gestalt of Rails, less so. I applaud Obie for trying to help you on this journey. Enjoy it.
—David Heinemeier Hansson
creator of Ruby on Rails
Foreword to the Previous Edition
by Yehuda Katz
From the beginning, the Rails framework turned web development on its head with the insight that the vast majority of time spent on projects amounted to meaningless sit-ups. Instead of having the time to think through your domain-specific code, you’d spend the first few weeks of a project deciding meaningless details. By making decisions for you, Rails frees you to kick off your project with a bang, getting a working prototype out the door quickly. This makes it possible to build an application with some meat on its bones in a few weekends, making Rails the web framework of choice for people with a great idea and a full-time job.
Rails makes some simple decisions for you, like what to name your controller actions and how to organize your directories. It also gets pretty aggressive and sets development-friendly defaults for the database and caching layer you’ll use, making it easy to change to more production-friendly options once you’re ready to deploy.
By getting so aggressive, Rails makes it easy to put at least a few real users in front of your application within days, enabling you to start gathering the requirements from your users immediately rather than spending months architecting a perfect solution only to learn that your users use the application differently than you expected.
The Rails team built the Rails project itself according to very similar goals. Don’t try to overthink the needs of your users. Get something out there that works and improve it based on actual usage patterns. By all accounts, this strategy has been a smashing success, and with the blessing of the Rails core team, the Rails community leveraged the dynamism of Ruby to fill in the gaps in plugins. Without taking a close look at Rails, you might think that Rails’ rapid prototyping powers are limited to the 15-minute blog demo but that you’d fall off a cliff when writing a real app. This has never been true. In fact, in Rails 2.1, 2.2, and 2.3, the Rails team looked closely at common usage patterns reflected in very popular plugins, adding features that would further reduce the number of sit-ups needed to start real-life applications.
By the release of Rails 2.3, the Rails ecosystem had thousands of plugins, and applications like Twitter started to push the boundaries of the Rails defaults. Increasingly, you might build your next Rails application using a nonrelational database or deploy it inside a Java infrastructure using JRuby. It was time to take the tight integration of the Rails stack to the next level.
Over the course of 20 months, starting in January 2008, we looked at a wide range of plugins, spoke with the architects of some of the most popular Rails applications, and changed the way the Rails internals thought about its defaults.
Rather than starting from scratch, trying to build a generic data layer for Rails, we took on the challenge of making it easy to give any ORM the same tight level of integration with the rest of the framework as Active Record. We accepted no compromises, taking the time to write the tight Active Record integration using the same APIs that we now expose for other ORMs. This covers the obvious, such as making it possible to generate a scaffold using DataMapper or Mongoid. It also covers the less obvious, such as giving alternative ORMs the same ability to include the amount of time spent in the model layer in the controller’s log output.
We brought this philosophy to every area of Rails 3: flexibility without compromise. By looking at the ways that an estimated million developers use Rails, we could hone in on the needs of real developers and plugin authors, significantly improving the overall architecture of Rails based on real user feedback.
Because the Rails 3 internals are such a departure from what’s come before, developers building long-lived applications and plugin developers need a resource that comprehensively covers the philosophy of the new version of the framework. The Rails™ 3 Way is a comprehensive resource that digs into the new features in Rails 3 and perhaps, more important, the rationale behind them.
1. Omakase is a Japanese term used at sushi restaurants to leave the selection to the chef. To learn more about the Omakase stack, read http://words.steveklabnik.com/rails-has-two-default-stacks