I’ve now made it through Chapter 2 and thought I’d comment on it here.
If Chapter 1 was “Ruby for Dummies,” then chapter 2 starts out as “Rails
for
Dummies,” but doesn’t stay that way. The beginning is a discussion of
the
classic MVC architecture and how the Rails framework libraries map to
the
various parts. Since my development box is a Win XP machine, my
directory
structure is a bit different from the one shown, but similar enough that
the
mappings were clear.
Where life starts to get really interesting is the development of the
sample
app (R4RMusic, short for Ruby for Rails Music Store) that is used in the
rest of the book. As many Rails developers apparently do, the author
started with the database and worked from there. The database consists
of
three tables and the associations that come with them.
One difference between this book and the others I’ve read is that the
SQL
provided to create the database in this case does not try to enforce
foreign
key relationships. That’s of course done in the Ruby code, using
belongs_to
and has_many relationships (interestingly, the sample does not use an
HABTM
relationship at this point), but the SQL creating the tables only lists
items like “composer_id” which will handle the relationship, but doesn’t
include any foreign key constraints in it. The AWDWR book (Thomas and
DHH)
has an extended discussion of this issue and makes it very clear that
the
true nature of relationships can’t be inferred from the database, but
even
in their book they add in foreign key constraints in their SQL.
Rather than try to download the code (a download is mentioned more than
once, but I don’t know if it’s yet available – besides, I wanted the
practice) I typed everything in by hand. I used RadRails 0.5.3 inside
Eclipse 3.1.1, incidentally, but that’s the only deviation I made from
the
demonstration itself.
Another major deviation from other Rails books is that the author does
not
immediately use the scaffold generator. Instead, the “generate model”
and
“generate controller” scripts were used repeatedly. Since generating
the
controller winds up generating the view as well, the basic files were
then
present for editing. The only difference I noticed is that the basic
CRUD
functionality wasn’t there any more.
Minor typo: In Listing 2.2, the second INSERT INTO line has two
“VALUES”
key words in it, one at the end of the first line and one at the
beginning
of the second line.
The chapter then shifts to the controllers and illustrates how easily
one
can use a finder class method to access all the records in each table.
I
found it notable that in the “Composer.find(:all)” method, the author
chose
to do sorting by appending a Ruby block instead of using the “:order”
symbol
in the find. Presumably something like “Composer.find(:all, :order ==>
“last_name, first_name”)” would have worked just as well as the block,
but I
haven’t tried it yet.
The controllers each include a “show” action which sets the relevant
entity
(@work, @edition, or @composer) using the “:id” param. Then we switch
to
the view.
The description of the view included a base layout, which was saved as
an
rhtml file, then the line “layout ‘base’” was added to the
application.rb
controller. The author mentioned that the layout could have been saved
as
“application.rhtml” directly and avoided this, but I agree it was useful
to
see the separation.
Listing 2.3 has a typo, by the way. Presumably the lines
“”
were meant to be included at the bottom of the listing, especially since
the
DOCTYPE was for XHTML 1.0 Strict.
The views themselves were all clear and easy to understand. They simply
used some erb to loop through the entities and display them, with
appropriate links.
Another unusual part of the demo is that rather than generate an index
method and thereby use the index view, the author created a separate
welcome
page and then had to edit routes.rb to make the application go there by
default. That’s not a problem, of course, and it was actually
interesting
to see how that was done. I just found it unusual relative to the other
books.
The last few pages of the chapter are the fun part. That’s where the
author
walks the reader through the actual call sequence, even separating out
the
web server calls (he uses Apache as an example) from the internal Rails
calls. I found that quite good, though I was expecting him to use the
same
R4RMusic application as an example rather than talking about a
completely
unrelated one.
In conclusion, I really liked this chapter. It’s true that parts of it
felt
overly basic or simplistic, but the demo worked and the walkthrough at
the
end was interesting.
I’m looking forward to digging into Chapter 3, now that I know it’s
available.
Ken
–
Kenneth A. Kousen, Ph.D.
President
Kousen IT, Inc.
http://www.kousenit.com
[email protected]