Book Highlights: Learning Rails 3

Learning Rails 3 by Simon St. Laurent and company is available from O’Reilly with a 4.3 out of 5 star rating. It’s a good book for beginners and anyone else who wants to relax reading about rails. Here are highlights I found interesting, along with sample code excerpts to serve as a quick reference and to give you an idea of what you can read more about by purchasing the book.

pXI. There’s a lot of seemingly magical behavior in Rails that works wonderfully — until one of the incatations isn’t quite right and figuring out what happened means unraveling all that work Rails did. Rails certainly makes it easier to work with databases and objects without spending forever thinking about them, but there are a lot of things to figure out before that ease becomes obvious.

p36. If there is a layout file in /app/views/layout that has the same name as a view, Rails will use that instead of application.html.erb.

p120. The after_save method is one of several callback methods support by ActiveRecord. Note that there are after and before methods for create, destroy, save, update, validation, validation_on_create, and validation_on_update.

p119. Chapter 8. Examples of using Form Builders. 

<%= form_for(@person… :builder => TidyFormBuilder) do |f| %>


p141. Passing block to collect() on building a Select.

<div class=”field”>
<%= f.label :student_id %><br />
<%= :student_id, Student.find(:all , :order => “family_name,
given_name”).collect {|s|
[(s.given_name + ” ” + s.family_name),]} %>

p161. A convenience method checks to see whether a given student is enrolled in a specified course:


def enrolled_in?(course)

The enrolled_in? method uses the include? method of courses…

A similarly useful convenience method returns the list of courses that a student is not yet enrolled in, making it easy to create logic and forms that will let them enroll:

def unenrolled_courses
Course.find(:all) –


This one-liner does some tricky set arithmetic

p176. Migrations are effectively lists of changes. Each migration is a set of instructions that applies to the results of the previous migration.

p177. Using Rails Console. You can use the y method (for YAML, a convenient data exchange format):

ruby-1.9.2-p136 :005 > y student
— !ruby/object:Student
id: 2
given_name: Milletta
middle_name: Zorgas

Also examples on using the app convenience object in IRB and includes.

> include Rails.application.routes.url_helpers

Then you can do things like:

> new_course_path
=> “/courses/new”

> app.get “/students/2”
=> 200 (for successful request)

> app.controller.params
=> {“action”=>”show”, “controller”=>”students”, “id”=>”2”}

> app.response.body
=> “<!DOCTYPE html>n<html>n<head>n
<title>Students</title>n <link href=”/assets/application.css?body=1” media=”all”…

> app.headers
=> {“Content-Type”=>”text/html; charset=utf-8”, “X-UA-Compatible”=>”IE=”…

p195. Example of using the debugger in IRB.

p260. You might have a route that looks like:

match ‘:action/awards’, :controller => ‘prizes’

if Rails encountered a URI like:


Then it would route the call to the show method of the prizes controller.

Also on p260, example of Globbing.

p266. Something I personally use all the time: rake routes | grep something…

p276. Example of nested CSS and the use of variables in SASS. Advises removing *= require_tree from application.css.scss file and using the @import directive on the files you specify to share all the variables, mixins and nested CSS of imported style sheets.

p282. Sprockets is a library that began shipping with Rails 3.1 for compiling and serving web assets like JavaScript and CSS files. It also serves as a preprocessor pipeline.” …Sprockets gives rise to JavaScript and CSS becoming first-class citizens, all because of that preprocessor pipeline it adds to a Rails application.

p286. On Bundler and Gems. Even Rails itself is a gem—you remember running “gem install rails,” right?

p293. CoffeeScript is a programming language, written in CoffeeScript, that compiles into JavaScript. Similar to the universal translator, you can write CoffeeScript code that is “translated” to JavaScript. If you’ve written JavaScript code, you’ll instantly see CoffeeScript’s appeal: it can spit out JavaScript with about one-third less code, it runs fast, and the clean syntax is similar to Ruby’s beautiful code.