tag:blog.hasmanythrough.com,2006-02-27:/tag/bookhas_many :through - book2007-12-20T04:31:00-08:00tag:blog.hasmanythrough.com,2006-02-27:Article/992007-12-20T04:31:00-08:002008-01-25T11:25:56-08:00Book Review: The Rails WayJosh Susser<p>Here's another entry in my ongoing Rails book review series.</p>
<p>Title: <a href="http://www.aw-bc.com/catalog/academic/product/0,1144,0321445619,00.html">The Rails Way</a><br/>
Author: Obie Fernandez<br/>
Publisher: Addison-Wesley</p>
<p>The latest entry in Addison-Wesley's Professional Ruby Series is <em>The Rails Way</em>, by Obie Fernandez. The name is a nod to Hal Fulton's noble classic <em>The Ruby Way</em>, and it's clear this book aims to be as significant a feature in the Rails publishing landscape. The good news is that the book delivers, and then some. <em>The Rails Way</em> appears destined to become the new bible of Rails development.</p>
<p>Before I dive into the full review, a disclaimer. Obie is a friend and fellow cabooser, and someone I have a lot of respect for. I just want to get that out of the way so that no one can say I'm shilling for his book on the sly. But I'm glad I can give him a good review with a clear conscience. Anyway, back to the review.</p><p>Here's another entry in my ongoing Rails book review series.</p>
<p>Title: <a href="http://www.aw-bc.com/catalog/academic/product/0,1144,0321445619,00.html">The Rails Way</a><br/>
Author: Obie Fernandez<br/>
Publisher: Addison-Wesley</p>
<p>The latest entry in Addison-Wesley's Professional Ruby Series is <em>The Rails Way</em>, by Obie Fernandez. The name is a nod to Hal Fulton's noble classic <em>The Ruby Way</em>, and it's clear this book aims to be as significant a feature in the Rails publishing landscape. The good news is that the book delivers, and then some. <em>The Rails Way</em> appears destined to become the new bible of Rails development.</p>
<p>Before I dive into the full review, a disclaimer. Obie is a friend and fellow cabooser, and someone I have a lot of respect for. I just want to get that out of the way so that no one can say I'm shilling for his book on the sly. But I'm glad I can give him a good review with a clear conscience. Anyway, back to the review.</p>
<p>The first thing you'll notice about <em>The Rails Way</em> is that it is simply enormous. At over 900 pages it is nearly twice the size of <em>Agile Web Development with Rails</em>. Quantity has a quality all its own, and the volume of this volume gives Obie room to go seriously in depth into just about every interesting aspect of Rails development. The size of the book makes doing a chapter-by-chapter analysis impractical. It even made doing a full read-through too daunting for me to attempt. So I did the review by reading a couple select chapters, then using the book as a reference for a couple weeks. Anytime I wanted to look something up in the API I used <em>The Rails Way</em> instead. There were a few odd corners of the API that weren't covered, but in general for everything I had to look up, the book had some useful information for me. That says something about its breadth. But it goes deep too. Obie doesn't just mention a feature and brush you off with a brief description of the API. He talks about the feature, puts it in context, sometimes talks about its history and evolution, says how you should use it, how you shouldn't use it, and when there is a better alternative. It's sort of like being tutored by a Rails expert with logorrhea (but in a good way).</p>
<p>As I said, an analysis of each chapter would be more than I can manage here, but the table of contents itself will give you an idea of the ambitious scope of the book:</p>
<ol>
<li>Rails Environments and Configuration</li>
<li>Working with Controllers</li>
<li>Routing</li>
<li>REST, Resources and Rails</li>
<li>Reflecting on Rails Routing</li>
<li>Working with ActiveRecord</li>
<li>ActiveRecord Associations</li>
<li>ActiveRecord Validations</li>
<li>Advanced ActiveRecord</li>
<li>ActionView</li>
<li>All About Helpers</li>
<li>AJAX on Rails</li>
<li>Session Management</li>
<li>Login and Authentication</li>
<li>XML and ActiveResource</li>
<li>ActionMailer</li>
<li>Testing</li>
<li>RSpec on Rails</li>
<li>Extending Rails with Plugins</li>
<li>Rails Production Configurations</li>
<li>Capistrano</li>
<li>Background Processing</li>
<li>Appendix A: ActiveSupport API Reference</li>
<li>Appendix B: Rails Essentials (plugins and other tools)</li>
</ol>
<p>You can see that the topics include things that aren't part of Rails itself, but things that everyone needs to do with Rails. It also documents parts of Rails that aren't well-documented anywhere else. Talk about exhaustive, I'm tired just from looking at that list of chapters.</p>
<p>The second thing you may notice is the cheery yellow stamp on the cover that says, "Covers Rails 2.0". Obie's been working on this book for ages, but somehow he managed to time it to hit the shelves within a week or two of the release of Rails 2 itself. I'm sure keeping up with all the changes and occasional API flailing was a major challenge, but it's great to have a thorough treatment of Rails that will have some staying power. Of course, Rails is a fast-moving target, so there are some details that will need to be corrected in the second printing. But don't let that stop you from getting the book now; it's worth it even with the minor blemish here and there. And if you're worried stuff might not get corrected, just take a look at the <a href="http://awprorubyseries.lighthouseapp.com/projects/6454-the-rails-way/overview">bug tracking site</a> for the book - they are on top of it!</p>
<p>Speaking of corrections, if you have a copy of the first printing (which would be everyone who has the book right now), the epigraphic quote by me at the start of Chapter 7 should say "reify" instead of "rarefy". Which is a nice segue into my next observation. The biggest strength of Rails is the community of people who use and contribute to it. So it's appropriate that Obie has invited some well-known Rails developers to contribute material to his book. He also has included many relevant quotes and commentaries from various developers (including a couple from yours-truly) and references to supporting software and writings. This strengthens the book in two ways. It adds important information, and it connects Rails to its ecosystem. As anyone who has gotten into Rails knows, trying to learn and use Rails in a vacuum is pointless. Learning about the people in the community and the extras that make Rails even more useful can be just as important as learning the API.</p>
<p>I'd like to say more about this book, but I think I've hit all the important points. I'll close by saying that on top have having a ton of information, <em>The Rails Way</em> is very well-written. It's well-organized, nicely structured, and the text itself is very readable. And the persnickety editor in me is even satisfied with the index.</p>
<p>Go get yourself a copy of <em>The Rails Way</em> and have fun upgrading to Rails 2.</p>tag:blog.hasmanythrough.com,2006-02-27:Article/952007-10-29T00:11:00-07:002008-01-24T00:19:36-08:00Book Review: Pro Active RecordJosh Susser<p>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, <em>Pro Active Record</em>.</p>
<p>Title: <a href="http://apress.com/book/view/1590598474">Pro Active Record: Databases with Ruby and Rails</a><br/>
Authors: Kevin Marshall, Chad Pytel, Jon Yurek<br/>
Publisher: <a href="http://apress.com/">Apress</a></p>
<p>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.</p><p>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, <em>Pro Active Record</em>.</p>
<p>Title: <a href="http://apress.com/book/view/1590598474">Pro Active Record: Databases with Ruby and Rails</a><br/>
Authors: Kevin Marshall, Chad Pytel, Jon Yurek<br/>
Publisher: <a href="http://apress.com/">Apress</a></p>
<p>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.</p>
<h2>The Good</h2>
<p>Let's start by taking a look at what the book provides. As I said, it covers all the basics.</p>
<p><em>Pro Active Record</em> 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.</p>
<p>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. </p>
<p>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).</p>
<p>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.</p>
<p>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.</p>
<p>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 <code>Test::Unit</code>. 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.</p>
<p>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 <code>find_by_sql</code> 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.</p>
<p>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!).</p>
<p>Finally, there is an Appendix that documents the core ActiveRecord API.</p>
<p>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.</p>
<h2>The Bad (and The Ugly, because you expected it)</h2>
<p>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.</p>
<p>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.</p>
<p>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 <em>Pro Active Record</em> 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.</p>
<p>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.</p>
<p>More seriously though, there are some glaring factual errors. For example, Chapter 2 states that <code>ActiveRecord#update_attributes</code> doesn't run validations on the model. But it's a totally common use case to use <code>update_attributes</code> like so:</p>
<pre><code>def update
@user = User.find(params[:id])
if @user.update_attributes(params[:user])
redirect_to user_path(@user)
else
render :action => "edit"
end
end
</code></pre>
<p>That's even how it's done in the standard scaffolding now. If <code>update_attributes</code> didn't run validations, that code wouldn't work. Now, the <code>update_attribute</code> (note the singular) method does <em>not</em> 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.</p>
<p>The book also states that there is a standard <code>db:bootstrap</code> 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 <code>has_and_belongs_to_many</code>, 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 <code>find_by_sql</code> 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.</p>
<p>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</p>
<h2>Bottom Line</h2>
<p>If you're looking to get started with ActiveRecord, <em>Pro Active Record</em> 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.</p>
<h2>Reviewing the Review</h2>
<p>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?</p>
<p>I'm intending to do more of these (maybe one every month or two), so tell me what works and what doesn't.</p>