Book Review: Pro Active Record

— October 29, 2007 at 00:11 PDT


I've been wanting to start doing some book reviews for a while, so here goes. The folks at Apress have been kind enough to send me review copies of a couple books, so I'm going to start with one of them. First up, Pro Active Record.

Title: Pro Active Record: Databases with Ruby and Rails
Authors: Kevin Marshall, Chad Pytel, Jon Yurek
Publisher: Apress

My first thought at seeing this book was, "Hey, a book all about ActiveRecord! Cool!" It's nice to finally see a volume dedicated to ActiveRecord, since it's my favorite part of Rails. However, this book ends up being something of a mixed bag. There's a lot of good information in it, and it does a decent job of covering all the basics. There are even some excellent parts here and there. But the book also has some problems, so it's hard for me to give it an unequivocal recommendation. Nevertheless, it does have value and fills a needed spot, so it may be what you're looking for.

The Good

Let's start by taking a look at what the book provides. As I said, it covers all the basics.

Pro Active Record starts with a good introduction to ActiveRecord, going over a bit of the history, how to install it, and shows a small example program. The best part of this chapter is the section that shows all the configuration settings for the different database adapters. It's nice to see all that in one place.

Chapter 2 digs into the foundation of ActiveRecord and how it maps objects to SQL. It covers CRUD operations, transactions, and locking. I like how it is full of examples that show an AR API call and the resulting SQL.

Chapter 3 is entitled Setting Up Your Database, but it's mainly about using migrations. It does however start with a good summary of the main ActiveRecord conventions for table naming and structure. It would be even better to see a summary of all the magic fields in one place (e.g. features like counter cache and optimistic locking).

Chapter 4 is where things start to get interesting. This chapter gets into the meat and potatoes (or granola and tofu for us Californians) of ActiveRecord. The three main topics are Callbacks (what I call the life-cycle features), Associations, and Validations. This is where you start to get a feel for what using ActiveRecord is like. Every major feature comes with some code examples, which is a good thing, though as contrived examples go some of them are pretty uninspired. Probably the biggest weakness in terms of content is that the section on associations doesn't show more than the most basic use cases. ActiveRecord associations are a rich topic and could easily cover a whole chapter, if not several. But for a beginner's book on ActiveRecord, this is probably enough. Though not seeing any mention of association extensions made me sad.

Chapter 5 is like the whole book in microcosm. Some good stuff, some stupid stuff, but not a waste of time. It covers some "bonus features" of ActiveRecord. The section on observers was an interesting start, but the topic deserves more than just one page. On the other hand, there are thirteen whole pages wasted covering the List, Tree and Nested Set features that everyone has known for ages were slated to be pulled from ActiveRecord (and were just extracted and turned into plugins). The section on aggregations is adequate, but the examples obfuscate more than they reveal. And yet, the chapter recovers and even shines with one of the best discussions of metaprogramming in Ruby and ActiveRecord that I've yet to read. Really, I'd almost say it's worth it to get this book just for the Extending Active Record section of this chapter. The thing that makes it work is how they show a problem, then an evolving series of solutions that do similar things but with more sophistication as they progress. At each step there is a discussion of the advantages and drawbacks of each approach. If the whole book was up to this standard, it would be a no-brainer to recommend it to everyone.

Chapter 6 introduces ActiveRecord testing and debugging. It includes a rather light discussion of writing tests and the basics of using fixtures. Most of the testing section covers the assertion methods in Test::Unit. While this book's focus isn't testing, it's a shame that there weren't at least a few good examples or some deeper how-to discussion. The world really needs a good book on testing Rails (so get off your butts, guys - you know who you are!). There follows a section that lists and describes the ActiveRecord exception classes. Finally there's a section on debugging tips, which is mainly about using logging. I was disappointed the chapter didn't include any mention of using irb (or the Rails console) for experimenting with code or debugging, and that there was no mention of using the Ruby debugger.

Chapter 7 addresses how to work with legacy databases. This is one of the trickiest things to do with ActiveRecord, because you don't get to go with the flow and have to do a lot more configuration than normal. The first part of the chapter describes the various configuration methods ActiveRecord provides to override its default conventions, and it's great to see them listed in one place like that. Then there is a slow dive into the bowels of ActiveRecord, covering find_by_sql and some of the low-level database APIs. There's also a section on import/export of your favorite interchange formats (xml, yaml, csv). What is missing is any discussion of how to map typical un-Rails-ish database usage patterns into ActiveRecord. For example, how to deal with non-integer primary keys, composite keys, foreign key constraints, enums, etc. That would have made this chapter a real winner.

Chapter 8 is the catch-all for everything that didn't fit in the other chapters. It has a section on reading the ActiveRecord source code that provides a roadmap for finding your way around. There's a decent discussion of various alternatives to ActiveRecord (other Ruby ORM packages), though it misses some recent additions to the field like Sequel and DataMapper. The FAQ section is pretty good, and provided a number of answers that were missing from previous chapters. One answer even includes some code from my own blog (thanks for giving the proper attribution!).

Finally, there is an Appendix that documents the core ActiveRecord API.

Looking back, that's a pretty good chunk of information on ActiveRecord, and probably enough to get anyone started. It's also very readable (though all the anecdotes about Kevin got to be a bit much). I thought the organization was good and followed a logical progression.

The Bad (and The Ugly, because you expected it)

Unfortunately, for all the book provides, there are enough problems with it that I have to wonder if it missed out on some much-needed editing.

To begin, this book doesn't say anywhere what version of ActiveRecord (or Rails) it is documenting. It makes me wonder if the authors were all on the same page here, because while most places seem to be working with Rails 1.2, some look like they are talking about Rails 2.0 code. Since Rails 2.0 is being released any day now, that's an important issue. There have been a lot of changes to ActiveRecord since Rails 1.2, and readers may be pretty confused about the applicability of the information. If nothing else, this book needs an update to get it in sync with Rails 2.0.

Next off, the book doesn't come with any downloadable example code. I've reached the point where I think that is a basic requirement for a software book. I think it's important to provide code that you can play with, or can use to copy/paste into your own code. It's also possible to provide a lot more code in the example projects, since putting all that code in the book is usually too much. And maybe most of all, having real code from which you grab excerpts for the book means that you can have confidence the code actually works. The code examples in Pro Active Record were obviously written in a word processor and never executed, since many of them wouldn't work correctly or even compile. I've messed up doing that in my own blog postings enough that I've learned that lesson.

One of the problems that affects the code samples disproportionately is that the book is riddled with typos and capitalization errors. It looks like Word's auto-capitalization messed a lot of stuff up badly, especially the example code. Unfortunately, when you're showing source code for a language that is case-sensitive, what might otherwise be a minor annoyance can become a significant issue. This is the sort of thing that is so easily prevented that I don't understand how it can happen.

More seriously though, there are some glaring factual errors. For example, Chapter 2 states that ActiveRecord#update_attributes doesn't run validations on the model. But it's a totally common use case to use update_attributes like so:

def update
  @user = User.find(params[:id])
  if @user.update_attributes(params[:user])
    redirect_to user_path(@user)
  else
    render :action => "edit" 
  end
end

That's even how it's done in the standard scaffolding now. If update_attributes didn't run validations, that code wouldn't work. Now, the update_attribute (note the singular) method does not run validations, but it is used quite differently from the plural form method. I could see how a casual reader of the ActiveRecord API might get confused, but not someone who had ever written a typical CRUD Rails application. Oddly enough, they get it right in the API summary appendix. Ah, the joys of collaborative authorship.

The book also states that there is a standard db:bootstrap rake task in Rails, which doesn't exist even in Rails 2.0 (though Rick Olson and others have rolled their own). It says you can explicitly delete object instances in Ruby, something that is impossible in a garbage collected language like Ruby. And it says the :id attribute is optional in has_and_belongs_to_many, where it will in reality break certain things if you include it. They also claim that it doesn't matter which ActiveRecord class you call find_by_sql on, when in actuality the class determines what kind of model object is returned from the call. Those are just some examples I caught; there are plenty of others.

This may all sound nit-picky, but I think if you can't have confidence in the accuracy of a reference book, then you can't truly rely on any of the information in it. Fortunately for the authors, none of these detail errors are major problems and they could be corrected with a good technical edit of the book. Perhaps they can be corrected in the ebook without waiting for a second printing

Bottom Line

If you're looking to get started with ActiveRecord, Pro Active Record is a pretty good place to start. It has everything you need to get your bearings and start your journey. Be advised, however, that it's not going to take you much past the start of that journey. And at least with the current edition, you'll need to double check the directions along the way.

Reviewing the Review

Since this was my first book review here, I'd like to get some feedback. What did you all think of it? Useful? Too critical? Too short? Too long? Not enough pie?

I'm intending to do more of these (maybe one every month or two), so tell me what works and what doesn't.

26 commentsactiverecord, book, rails, review

Comments
  1. bjc2007-10-29 00:42:53

    Sounds like you were going easy on an otherwise terrible book. Some of those errors sound beyond the pale.

  2. Doug2007-10-29 00:48:13

    Awesome - I really appreciate balanced reviews like this. Thanks.

  3. Don2007-10-29 00:56:58

    I'd say book reviews would be very useful to your readers, considering the number of books coming out these days that are Ruby and Rails related. Having somebody who knows the subject material review things positively is going to make me feel a lot better than a 5 star rating from "CoolSteve328" on Amazon.

  4. Josh Susser2007-10-29 01:21:17

    bjc: Sometimes even someone who's not a guru on a subject can write a decent book. With a good technical/copy edit, I think this book would be a fine beginner book on ActiveRecord. But you may want to wait for the second printing or an updated ebook.

  5. wilson2007-10-29 01:57:06

    Truly great books on sofware engineering are worth their weight in gold. The problem is that there are so many terrible ones cluttering bookshelves (and minds).

    Thanks for the great review!

  6. Larry Wright2007-10-29 02:45:42

    I like the format and thoroughness of the review. It's very obvious that you read the book thoroughly. A bunch of people (myself included) have gotten free review copies of Apress books lately, and as I have started seeing the reviews appear I've seen many that are so short (and so glowing), that I wonder if the reviewers read the books or merely skimmed them.

    I have done a number of book reviews, and I've adopted two basic styles. The first is the long-form review, similar to yours. An example is my review of Ship It!. The second is the short-form "what I've been reading" review. You can see the latest one here. These are a lot shorter, and subsequently a lot less in-depth. I think there's a place for both.

    All in all, I like the review and the format. Keep them coming!

  7. Josh Susser2007-10-29 02:55:34

    Larry: Thanks, that's good feedback. I like the idea of the short-form blurb reviews too. I may have to try that sometime.

  8. Neeraj Kumar2007-10-29 03:43:54

    I think Josh was easy on the book.

    It's my assumption that if you are buying this book solely dedicated to 'Active Record' then you are not 100% RoR newbie. With that assumption I bought the book and was a bit disappointed.

    I expected the book to cover some internal stuffs like all the magic that happens behind the scene. I still haven't had time to look at how the callback logic is actually implemented. There are hundreds of similar magic that happends in ActiveRecord and none of that was covered.

  9. JohnB2007-10-29 04:50:14

    Thanks Josh - sounds like I should hold off on this book.

    The book review that I really want written is about the book that hasn't (as far as I know) been written: a rails book that fully covers rails 2.0 - at a level applicable to both newbies as well as experts. Oh yeah, and world peace while you're at it...

  10. Giles Bowkett2007-10-29 08:00:47

    I definitely dig the review. Especially the detail. If anything, more detail.

  11. Matt Beedle2007-10-29 09:31:31

    I think this review format is excellent. I really liked the chapter breakdown. It sounds to me if someone new to rails bought this book in its current state, they would spend many frustrated hours googling for answers to the broken example code!

  12. Chris2007-10-29 09:57:59

    I'd buy a decent Pro ActiveRecord book

  13. Cristian R. Arroyo2007-10-29 11:09:48

    I was seriously considering buying the electronic version of this book. Your review has persuaded me no to. Thank you for a very well informed post.

    PS: I'd also buy a decent mid-level ActiveRecord book anytime.

  14. Chad Pytel2007-10-29 12:38:54

    Josh,

    Thanks very much for your very fair review of our book. Like Larry says above, its very obvious that you've thoroughly read the book, and I appreciate that. I have no choice but to agree with you that the book would be stronger given a more thorough technical review. Many of the problems that you mention (for example, the problem with update_attributes) were simple typos that a review should have caught. The publication cycle, along with other external factors and delays, didn't allow for as thorough a technical edit as was apparently needed.

    I don't want to take the time here to give an excuse for every technical error (although I'm pretty sure one exists). I can only say that it turns out writing a book is hard - you end writing something down thinking "oh, I'll come back and revise this" and then 3 months later, you've missed it.

    Hopefully, as you seem to indicate, the book's strengths outweigh the weaknesses, and we've provided a resource by bringing together a lot of information into one place for the people who need it. We're actively gathering up errata in order to get these corrections made as quickly as possible, as its unacceptable that some of them made it into the print version of the book, and we want to get them corrected right away.

    Also, the source code is now online at the book's web page - apress just got it posted, so it probably wasn't there when you were writing the review. Just click on "Source Code" in the Book Extras sidebar.

    thanks again for the review, -Chad Pytel

  15. Julio2007-10-29 14:03:05

    I'm intending to do more of these (maybe one every month or two), so tell me what works and what doesn't.

    Overall great review. It could be a little shorter, but maybe because I'm in a hurry right this minute. Coincidentally I finally got this book last week. Now I'll finish it with your review in mind.

    (it's fun to review reviews)

  16. zerohalo2007-10-29 16:56:49

    Thanks, Josh. Excellent review. Too many tech book reviews are too short to get a real feel for what's inside the book and whether it meets my particular needs or interests.

  17. Rabbit2007-10-29 17:18:40

    I dig the review. Especially, as others have mentioned, the detail. The bit on metaprogramming and their process of evolving a plugin sounds tasty, and I think I'll bite for that information alone.

    And actually, despite the acts_as_xxx bits being taken out of core, I'm looking forward to a thorough writeup regarding them.

    If I weren't seasoned (ha!) the errors (e.g. update_attributes bit) might scare me more than they do, but I agree with bjc that some of those errors are pretty bad, especially for beginners.

    Thanks for the writeup. Keep'em coming.

  18. Bob Aman2007-10-29 17:33:09

    Good review. Apress sent me a copy too for review. I haven't posted the review yet because they sent me two books and I still haven't finished the second yet, but your review was a lot kinder to the book than mine was. I basically said that Chapter 5 was the only part of the book worth reading and you should just buy AWDWR instead.

  19. ChrisB2007-10-29 19:54:08

    I'm not an experience programmer and therefore didn't think it would be fair to write a negative review on Amazon. So I expressed my disappointment with this book in a review on Facebook's Visual Bookshelf a few days after the book was released. I then thought I might have missed something about the book when the glowing reviews appeared on Amazon. So it's great to have my suspicions confirmed by an expert that the book maybe really isn't all that good.

  20. Chris2007-10-31 16:49:59

    I can say that after reading this review, I'll be looking forward with much anticipation of your future book reviews. Kudos.

  21. Michael Slater2007-11-05 15:56:07

    Great review. It is very helpful to have someone of your expertise posting such a detailed review. I skimmed through the book and noticed a few of the errors, but there are some that I would not have recognized as such and could have been led astray.

  22. James Wylder2007-11-10 17:57:12

    I bought this book hoping for more than it was. Honestly, I was hoping for something like your blog expanded to cover all of active record. I thought the book didn't give me a lot more than the Agile Development chapters. Your blog has given me more than I got from this book.

  23. Nate Klaiber2007-11-15 16:31:39

    Josh, Thanks so much for the thorough review. I had been looking at this book and dropped it in my wishlist until I could investigate more. I scanned through it at a local Borders and was still on the edge of the quality and depth of the content. When I see a 'Pro' title I expect to be diving in deep to the AR API and advanced techniques for extending it.

    As far as the length of your review, I thought it was perfect. I would rather have a detailed review that lets me get a good glimpse of the book - than a short rave of a book. Even if you had no qualms with the book, having a detailed review is much better than skimping.

    Thanks for the great review, and I hope to see more to come in the future!

  24. John Joyce2007-11-16 01:54:03

    Great review. Thorough, balanced, with a nice sense of what perspectives potential readers might have. Very useful to me. The basics... don't need that (well maybe for Rails 2.0, to learn it again) but why does Apress give these stupid names to books? (Beginning... From Novice to Professional, Pro..., Expert... ) They're rarely accurate, though Apress tends to have nice books... Anyway, I now know my browsing of this book was correct, it's not what I want or need. The lack of code samples is a big problem. Nobody can touch Learn to Program by Chris Pine with LIVE code samples. (even if it is for beginners, you KNOW the code samples work) Well, maybe OREILLY can put out an in a nutshell book on Rails 2 or ActiveRecord ...

  25. Jeppe Liisberg2007-11-23 10:14:22

    On October 28, 2007, JohnB wrote:

    The book review that I really want written is about the book that hasn't (as far as I know) been written: a rails book that fully covers rails 2.0 - at a level applicable to both newbies as well as experts. ...

    I'd certainly recommend "The Rails Way" by Obie Fernandez - It's Rails 2.0, was published a few days ago and is pretty thorough as far as I can see. I'm still reading though - and a beginner with rails, so my opinion might not weigh that much. But check it out!

  26. Josh Susser2007-11-25 20:01:41

    Jeppe: I've got my review draft of Obie's book, and it's next on my list to cover. The only thing is that it's big enough to knock a small moon out of orbit, so it's going to take me some time to work through it...

Sorry, comments for this article are closed.