A Note About Rails Versions 3

Jeffrey Hardy,
Dec 18, 2007

As some of you may be aware, Rails 2.0 was recently released. This is a huge milestone for the Rails project, but it introduces a few changes that are sure to trip up readers.

The book was written for Rails version 1.2.3

That’s right. When the book was written, Rails 2.0 was merely a glint in the core team’s eye.

Generally, a release that increments the major version number (as in Rails 1.x to 2.0) will include breaking changes, and Rails 2.0 is no exception. With this latest release, the core team has been really aggressive in terms of removing features. Some of the nice-to-have features like scaffolding and pagination that we wrote about in the book are no longer part of the core framework but are available as plugins instead.

Installing and using Rails 1.2.3

If you want your sailing to be smooth, I recommend installing and using Rails 1.2.3 as you go through the examples in the book. Here’s how you would go about installing version 1.2.3 via RubyGems and telling your application to use it:

  1. sudo gem install rails -v 1.2.3
  2. open config/environment.rb and make sure the RAILS_GEM_VERSION is set to 1.2.3:
# Specifies gem version of Rails to use when vendor/rails is not present
RAILS_GEM_VERSION = '1.2.3'

You’ll no longer need to worry about installing the scaffolding or pagination plugins because they’re both alive and well in Rails 1.2.3.

You can also elect to freeze a copy of Rails into your project’s vendor directory, which will take precedence over the Gem version you’ve specified. Rails makes this easy with a bundled Rake tasks:

$ rake -T | grep freeze
rake rails:freeze:edge  # Lock to latest Edge Rails or a specific revision with REVISION=X
rake rails:freeze:gems  # Lock this application to the current gems (by unpacking into vendor/rails)
rake rails:unfreeze     # Unlock this application from freeze of gems or edge and return to a fluid use of system gems

As you can see from the comments, you can freeze to either the edge version of Rails, or a specific revision from the repository. You can also freeze to whatever Gem version of Rails you have installed on your system.

This is often called the vendor everything approach.

What about the future?

Take heart. Most of the changes are minor enough that you won’t have any trouble transferring your knowledge to Rails 2.0. By the time you’ve finished the book, you’ll have a much better understanding of Rails, and to be sure, the fundamentals haven’t changed.

The new and refined features of Rails 2.0 have only made it better.

Source code and errata updated

Jeffrey Hardy,
Sep 11, 2007

I’ve updated (and corrected) the source code for the book.

At the request of readers, I’ve now included the individual code listings for each chapter (previously, I’d included only the finished, sample application in its entirety). In addition to the app, the code archive now contains all the listings, (named for their listing numbers and filename), divided into directories by chapter. So, if you’re looking for Listing 3-5 from Chapter 3, you’ll find it in chapter03/3-5-event.rb. Observe:

j:~/book/source$ ls
README      chapter01   chapter03   chapter05   chapter07   chapter09   chapter11
application chapter02   chapter04   chapter06   chapter08   chapter10

j:~/book/source$ tree chapter03
chapter03
    |-- 3-1-database.yml
    |-- 3-2-001_create_events.rb
    |-- 3-3-001_create_events.rb
    |-- 3-4-events_controller.rb
    |-- 3-5-event.rb
    `-- 3-6-events_controller.rb

0 directories, 6 files

You get the idea.

This should make things a lot easier if you’re following along with the code in the book. Also note that I’ve been able to correct all the bugs/typos/errors in the code (which is much more forgiving than print). The errata list is nearly complete too, so if you’re reading the book you’ll want to check that out lest you get tripped up by something.

The source code and errata will always remain available at these locations:

The permanent links are over there in the sidebar.

Unknown database bug in Chapter 2 1

Jeffrey Hardy,
Aug 14, 2007

Or, Rails without a database.

I found the first bug in the installation instructions for Beginning Rails. If you’re having trouble running the inaugural application (aptly named hello), it’s probably because we forgot to mention that Rails expects a database.

Without the hello_development database, browsing to http://localhost:3000/salutation/hello will result in something like this:

Mysql::Error (#42000Unknown database 'hello_development'):
/usr/local/lib/ruby/gems/1.8/gems/activerecord/lib/active_record/vendor/mysql.rb:523:in `read'

In the interest of making apparent the irony, I draw your attention to the first paragraph of the chapter in question:

For various reasons, Rails has gained an undeserved reputation of being difficult to install. We want t dispel this myth.

Well, so much for dispelling the myth!

Remember how in the installation instructions we said that Rails was downright stubborn about working without a database? Well, we were right. Here Rails is obviously expecting a database called hello_development, but we didn’t create one!

There are two ways to fix this problem. You can either:

  1. make Rails happy by creating the required database, or
  2. edit Rails’ config file and tell it you’re not using a database for this application

Creating the database is perhaps the easiest way to go. You can flip ahead a few pages and read up on how it’s done, starting on page 47. But, if you’re impatient like I am, (and if you followed the installation instructions), you should be able to create the hello_development database thusly from the command line:

mysqladmin -uroot create hello_development

Stop the server using CTRL-C, and restart it using ruby script/server. Reload http://localhost:3000/salutation/hello and see if it works. If it does, great. Let’s just pretend this little mishap never happened. If it doesn’t, we’ll have to edit the config file.

Rails’ main config file is located in config/environment.rb. To configure Rails not to bother with a database, open it up in your text editor of choice and find the (commented out) line that reads:

# config.frameworks -= [ :active_resource, :action_mailer ]

Remove the comment and modify the line so that it excludes only ActiveRecord.

config.frameworks -= [ :active_record ]

Again, stop the server using CTRL-C, and restart it using ruby script/server. Reload http://localhost:3000/salutation/hello and all should be well.

Apologies for this ironically placed blunder!