Crafting Rails Applications: Why I wrote this book

When I first got into Ruby and Rails development, I was impressed about how everything worked. A few lines of code and so much got done. At that time, if I was reading a book or blog post that said: “just do X and Y will automagically work”. I would gladly accept those instructions.

However, after a while, I wanted to understand how it really worked from the inside, so I started reading the source code from different plugins, including Rails itself. Accusing magic to be the reason for that 200 lines of code to “just work” didn’t feel right anymore.

Turns out, I was not the only one with this feeling. I have been into a few big conferences this year and asked people outside the Rails community what they think about it. And, for my surprise, some of them mentioned that “there is too much magic going on”.

We have been victims of myths before, as the famous “Rails can’t scale” one, and we strongly fought back. But what about the “there is too much magic going on”? Is it myth? Who is there to blame?

Yes, it is a myth and we (the Rails community) are the ones to blame for spreading it.

I have already seen people explaining how “has_many :comments” works as magic: “it magically guesses the columns from your database”. Well, a more appropriate description for that would be “Convention over Configuration”. Rails uses a set of conventions so it can properly build your associations without you having to specify all tables and columns.

Heck, I also did my part in the past. There was a time that Inherited Resources’ README was filled by words like “magic” and  “automagically”. Just recently, I replaced all this occurencies with more detailed explanations of how it really works.

The question is: isn’t it about time for us to start fighting against the “magic” myth?

When I improved Inherited Resources’ README, the benefits were clear in my head. People using Inherited Resources would write better code and be more productive because they understood it better. And, as consequence, they would be able to troubleshoot issues easily or even contribute back. This is clearly a win-win situation.

At the beginning of this year (or more specifically February, 2010) I started to put a lot of effort in writing and understanding Rails 3 architecture. Or as some people would (wrongly) say: I started to unveil the magic. After some time, it was clear that I could write better and faster code and that a lot of people could benefit from this knowledge as well.

Now, 10 months later, I am happy to say that the beta version of my book Crafting Rails Applications is out to spread what I have learned about Rails and its applications. Rails is a framework that allows us to write web applications and an advanced Rails book should consequently be about crafting them.


Crafting Rails Applications Cover

Crafting Rails Applications guides you through seven different tutorials, each of them using test-driven development to build a new Rails extension or application that solves common problems through the new Rails 3 APIs. You will understand how the Rails rendering stack works and customize it to read templates from the database while you learn how to mimic Active Record behavior, like validations, in any other object. You will find out how to write faster, leaner controllers, and you’ll learn how to mix Sinatra applications into your Rails apps, so you can choose the most appropriate tool for the job. In addition, you will improve your productivity by customizing generators and responders.

From the book cover:

Rails 3 is a huge step forward. You can now easily extend the framework, change its behavior, and replace whole components to bend it to your will, all without messy hacks. This pioneering book is the first resource that deep dives into the new Rails 3 APIs and shows you how use them to write better web applications and make your day-to-day work with Rails more productive.

My goal is that Crafting Rails Applications increases the Rails community thirst for knowledge, that “magic” is no longer enough to describe any part of Rails or its ecosystem.

The benefits are clear: leaner code, faster applications and a well-grounded Rails community. Check what people are saying or get a copy on Pragmatic Programmers web site.

  • http://nandovieira.com.br Nando Vieira

    Fuck yeah! Finally a book with some advanced topics.

  • http://twitter.com/collinschaafsma Collin Schaafsma

    Just finished the book over the weekend. I have a much better understanding of the changes in ActionController and writing engines. It’s a great book! Thanks so much José! Excellent work.

  • http://twitter.com/collinschaafsma Collin Schaafsma

    Just finished the book over the weekend. I have a much better understanding of the changes in ActionController and writing engines. It’s a great book! Thanks so much José! Excellent work.

  • http://binarysoul.com Eric Himmelriech

    Nice! I’ll be checking this out

  • http://twitter.com/danielpalacio danielpalacio

    Great to see a book that is a little more advance, but could you please share the first chapter of the book for free. I hate buying books to discover that it’s not what I was looking, or I dislike how the author writes. You could publish the preface and chapter 1 for free so anyone can download(without registering etc).

  • http://twitter.com/fakingfantastic Frank Lakatos

    A comment about “Magic”

    Don’t forget that the big allure to Rails is the magic: good or bad. I would dare say that all the members of the Rails community grew because of the magic they initially saw it offered. With that said, I am completely with you that folks need to start diving deeper, start hacking, and learn how mature and sophisticated the Rails codebase is. People need to see how intricate it is, so they do not take it for granted; they respect it. But remember, just like with magic – just because you know how the tricks are done, doesn’t mean it stops being fun to witness!

  • http://blog.plataformatec.com.br/ josevalim

    You can get a few extracts (like half of the first and part of the fourth chapter) on the webpage at Pragmatic Programmers: http://pragprog.com/titles/jvrails/crafting-rails-applications

    I hope you enjoy it and I would be eager to hear your feedback!

  • http://blog.plataformatec.com.br/ josevalim

    Glad you enjoyed, thanks!

  • http://twitter.com/twmills Theo Mills

    Just bought it and I’m really looking forward to reading it.

  • http://rosenfeld.heroku.com/ Rodrigo Rosenfeld Rosas

    Que boa surpresa! Vou comprar meu e-book agora mesmo. Será meu presente de Natal ;)

  • http://twitter.com/aaronblohowiak aaron blohowiak

    Convention over configuration: sane default values. magic: changing the scope in which a block is eval’d so you can inject BlankSlates that have method_missing defined on them that then do some string manipulation in order to pick which code path to take.

  • Anonymous

    Good article, Valim.

    Check out Docco.js for documenting code. You might find that you can use it to help your readers through the code examples and activities in your book. This is worth looking into as the big weakness in the Pragmatic Programmers series is their site. The forums and wikis are poorly frequented and stuff gets out of date on their too fast and posting questions on their is an exercise in futility, since few people frequent the site often enough to answer questions in a timely manner.

  • http://twitter.com/kirkvq Kirk Quesnelle

    Just picked this up.. cannot wait to dig into this.

    Thanks José

  • http://twitter.com/kirkvq Kirk Quesnelle

    Just picked this up.. cannot wait to dig into this.

    Thanks José

  • http://twitter.com/michaelreed_org Michael Reed

    Just bought the beta of your book. Thanks. 3 months into learning rails after a decade of web + other stuff development and I love it. Thankful for the magic and for the education about what’s behind it.

    I also appreciate devise.

  • http://pulse.yahoo.com/_MQY62OW2UB47SBX5XLMRHC7NDI Endy

    Magic is technical debt, it gets you through tight spots in the beginning, but later you have to study the magic to get further.