Kevin Marshall, Chad Pytel, and Jon Yurek, Pro Active Record: Databases with Ruby and Rails (2007). $39.99. [Amazon]
Pro Active Record is all about ActiveRecord, the object-relational mapping layer that comes with Rails. In this review I’ll call ActiveRecord AR. The book has chapters on SQL, setting up your database, the core features of AR, extra AR goodies, testing and debugging, working with legacy schema, AR and the real world, and, finally, a summary of the AR API (interestingly, the API summary is not a copy of the official docs, but has additional comments from the authors, so there is some real added value in this “back of the book” reference material).
Before I begin I want to note that these kinds of framework books and chapters are really hard to write (I know: I contributed to the O’Reilly JavaEE book). The reason is that a lot of readers come to a subject such as AR without understanding the problem for which AR is the solution. I.e., if you don’t already have some understanding of relational databases and SQL, and maybe have some experience with different RDBMS from more than one vendor, some of what’s going on in AR and in this book can be lost.
Generally I would say that if you do any non-vanilla work with AR, you should own this book as a supplement to the account of AR in Agile Web Development with Rails. There is genuinely useful information throughout the text. Here are a few examples that got a check or exclamation point in the margin while I was reading: executing migrations outside of Rails (p. 49); dealing with migrations in source control (p. 53); a nice discussion of writing good tests (pp. 127ff); more detail on the more obscure test assertions than I’ve seen elsewhere (pp. 129-139); a discussion of transactions and fixtures (p. 141); how to do .csv fixtures (pp. 142-143 — annoying, by the way; this could be a lot better in AR); good stuff on AR exceptions (pp. 144ff); random notes on legacy schema integration (chapter 7), internationalization (p. 204), use of UUIDs for PKs (p. 205), and some canned associations for typical use cases (pp. 208-209).
The book also handles well the true core of AR, setting up associations between models, and validating models (chapter 4, “Core Features of ActiveRecord”). The example “domain” is about managing Farmers, Cows, and Milk, which I found just weird and kinky enough that I learned something. A peculiarity of this chapter is that it kicks off with Callbacks rather than Associations and Validations, so a beginning reader is going to have to wait before getting to the core of the core. But I digress.
A brief tour de force is a detailed example of extending AR with Ruby meta-programming (pp. 109-123). This is one of the longer discussions I’ve seen anywhere regarding AR that provides insight into how it actually works, and how you create your own convenience apparatus for easy-to-write queries.
Now some less good news. The book’s title makes it sound as though it is going to be complete and exhaustive, but it is not. There are lots of little things that are missing. Just for example, I couldn’t find a discussion of :source for the has_many :through association, which is necessary in some cases and has vexed my students. In fact, the chapter that gives the API doesn’t even document ActiveRecord::Associations, which is a major gap (to be sure, there is much information here on associations, but if you’re going to detail the API for ActiveRecord::Base, why leave out stuff as important as ::Associations?). Another aspect that is missing is dealing with some of the cases where you need to have multiple belongs_to foreign keys: AR can get confused, and it would be helpful if someone played this out at some length; this book doesn’t do that. Elsewhere, mention is made of a discussion of SQL injection (p. 150), but that discussion never appears.
Had the title been something like “ActiveRecord Techniques” I’d be less concerned about these gaps. I hope they’ll be addressed in a second edition that is larger and more capacious: We need a single-volume “big book” on AR, and this could be it with some additional work.
In sum: Good book! Great bits! 2nd edition has potential to be awesome.