Rails 1.1: The Forgotten Features

Today, Mike Clark follows the release of Rails 1.1 with: Rails 1.1: The Forgotten Features, a writeup of some of the little changes in Rails 1.1 that, though not big enough to make the big list of "wow"s, are sure to make all Rails developers’ lives a little better every day.

Take a look and feel free to suggest additions to the list if you’ve got ’em.

Rails 1.1: RJS, Active Record++, respond_to, integration tests, and 500 other things!

The biggest upgrade in Rails history has finally arrived. Rails 1.1 boasts more than 500 fixes, tweaks, and features from more than 100 contributors. Most of the updates just make everyday life a little smoother, a little rounder, and a little more joyful.

But of course we also have an impressive line of blockbuster features that will make you an even happier programmer. Especially if you’re into Ajax, web services, and strong domain models — and who isn’t these funky days?

The star of our one-one show is RJS: JavaScript written in Ruby. It’s the perfect antidote for your JavaScript blues. The way to get all Ajaxified without leaving the comfort of your beloved Ruby. It’s the brainchild of JavaScript and Ruby mastermind Sam Stephenson and an ode to the dynamic nature of Ruby.

Here goes a few sample rjs calls:

  # First buy appears the cart, subsequent buys highlight it
  page[:cart].visual_effect(@cart.size == 1 ? :appear : :highlight)
  # Replace the cart with a refresh rendering of the cart partial
  page[:cart].replace_html :partial => "cart"
  # Highlight all the DOM elements of class "product"
  page.select(".product").each do |element|
    element.visual_effect :highlight
  # Call the custom JavaScript class/method AddressBook.cancel()
  # 4 seconds after rendering, set the font-style of all company
  # spans inside tds to normal
  page.delay(4) do
    page.select("td span.company").each do |column| 
      column.set_style :fontStyle => "normal"

And that’s just a tiny taste of what RJS is capable of. It takes the Ajax on Rails experience far above and beyond the great support we already had. Bringing us even closer to the goal of “as easy as not to”. Read more about RJS in the docs or in Cody Fauser’s tutorial about element and collection proxies and his introduction to RJS (it shouldn’t surprise you that Cody is writing about book about RJS for O’Reilly).

But its not just the view we’re giving some tender love, oh no. Active Record has been blessed with bottomless eager loading, polymorphic associations, join models, to_xml, calculations, and database adapters for Sybase and OpenBase. It’s a huge upgrade and made possible through the fantastic work of Rick Olson (who was recently accepted into Rails Core, not a minute too soon!) and Anna Chan. Let’s dig into three of the top features:

Bottomless eager loading gives you the power of pulling back a multi-level object graph in a single JOIN-powered SQL query. Example:

  # Single database query:
  companies = Company.find(:all, :include => { 
    :groups => { :members=> { :favorites } } })
  # No database query caused:

You can mix’n’match too. Using both multi-level fetches and first-level ones in the same call:

  # Just 1 database query for all of this:
  authors = Author.find(:all, :include => [ 
    { :posts => :comments }, :categorizations ])
  authors[0].posts[0].comments[0].body # => "Rock on Rails!"
  authors[0].categorizations[0].name   # => "Less software"

Polymorphic associations and join models give you access to much richer domains where many-to-many relationships are exposed as real models. Say Authorship between Book and Author:

  class Author < ActiveRecord::Base
    has_many :authorships
    has_many :books, :through => :authorships
  class Book < ActiveRecord::Base
    has_many :authorships
    has_many :authors, :through => :authorships
  class Authorship < ActiveRecord::Base
    belongs_to :author
    belongs_to :book

…or addresses that can belong to both people and companies:

  class Address < ActiveRecord::Base
    belongs_to :addressable, :polymorphic => true
  class Person < ActiveRecord::Base
    has_one :address, :as => :addressable
  class Company < ActiveRecord::Base
    has_one :address, :as => :addressable

Now let’s have a look at the new respond_to feature of Action Controller that makes it much easier to launch your application with both Ajax, non-Ajax, and API access through the same actions. By inspecting the Accept header, we can do clever stuff like:

  class WeblogController < ActionController::Base
    def create
      @post = Post.create(params[:post])
      respond_to do |type|
        type.js   { render }  # renders create.rjs
        type.html { redirect_to :action => "index" }
        type.xml  do
          headers["Location"] = post_url(:id => @post)
          render(:nothing, :status => "201 Created")

The recently launched API for Basecamp uses this approach to stay DRY and keep Jamis happy. So happy that he wrote a great guide on how to use respond_to

Speaking of Jamis, he also added the third layer of testing to Rails: Integration tests. They allow you to faithfully simulate users accessing multiple controllers and even gives you the power to simulate multiple concurrent users. It can really give you a whole new level of confidence in your application. The 37signals team used it heavily in Campfire from where it was later extracted into Rails. See Jamis’ great guide to integration testing for more.

These highlighted features are just the tip of the iceberg. Scott Raymond has done a great job trying to keep a tab on all the changes, see his What new in Rails 1.1 for a more complete, if brief, walk-through of all the goodies. And as always, the changelogs has the complete step-by-step story for those of you who desire to know it all.

And as mentioned before, Chad Fowler’s excellent Rails Recipes has in-depth howtos on a lot of the new features. If you desire some packaged documentation, this is the book to pick up.

Upgrading from 1.0

So with such a massive update, upgrading is going to be hell, right? Wrong! We’ve gone to painstaking lengths to ensure that upgrading from 1.0 will be as easy as pie. Here goes the steps:

  • Update to Rails 1.1:
    gem install rails --include-dependencies
  • Update JavaScripts for RJS:
    rake rails:update

That’s pretty much it! If you’re seeing any nastiness after upgrading, it’s most likely due to a plugin that’s incompatible with 1.1. See if the author hasn’t updated it and otherwise force him to do so.

If you’re on Ruby 1.8.2 with Windows, though, you’ll want to upgrade to the 1.8.4 (or the script/console will fail). And even if you’re on another platform, it’s a good idea to upgrade to Ruby 1.8.4. We still support 1.8.2, but might not in the next major release. So may as well get the upgrading with over with now.

San Diego Ruby Users Group

There is some action down in San Diego on the Ruby and Rails front. The local RUG, after some time of inactivity, is revving back up. If you live in the area and are keen on meeting up with like minded Rubyists and Railers, take a look over at Kevin Clark’s blog for more information.

Rails for designers

Kevin Clark has written up Rails for designers, an intro to Rails directed at designers which focuses on the parts and concepts of the framework that are of most immediate relevance to a designer. The tutorial gets designers oriented and ready to find, edit and use an application’s templates. From layouts, to ERb, to partials and instance variables, this tutorial will get designers up and running in no time. If you are thinking of doing a Rails project, you could do a lot worse than having your designers start with this tutorial. Thanks for the primer Kevin.

Rails 1.1: Release Candidate 1 available

It’s been roughly three months since the release of the big one-oh. That’s obviously an eternity in Rails time, so its about high time we’re getting ready for the release for 1.1. And boy, is this an exciting upgrade!

I do believe this is the biggest upgrade to Rails we’ve ever done. We have recorded about 500 fixes, tweaks, and new features in the changelogs. That’s a lot and that’s just counting major new features like RJS as one.

So with all these goodies, we want to make sure we launch without any obvious blunders or backwards compatibility breaking changes. This is why we’re doing a release candidate and why we need your help to test it.

Rails 1.1 is supposed to be just fully backwards compatible with 1.0, but we did change just a couple of defaults, see CHANGED DEFAULT notes in the changelogs. That means we want to test Rails 1.1 with as many 1.0 applications as possible.

To install the release candidate gems, you just need to do:

gem install rake
gem install rails --source http://gems.rubyonrails.org

Or you can just install the new Rake gem (Rails 1.1 depends on Rake 0.7) and then call rake freeze_edge. That’ll pull the latest Rails down from the Subversion repository and bind just that one application to it.

Or you can set svn:externals on vendor/ to be against http://dev.rubyonrails.org/svn/rails/tags/rel_1-1-0_RC1, if you want to pull it in through Subversion automatically.

Lots of options, no excuses. We really need your help to make sure the final release is as solid as Rails 1.0 was. And so we don’t need 1.1.1 two days later.

Once you have the latest Rails installed, you can do rake rails:update to get the latest scripts and the latest version of Prototype and script.aculo.us installed in public/javascripts. That’s about all the upgrading you need to do to existing applications.

Do note, though, that all plugins may not be upgraded to be compatible with Rails 1.1. Or you may indeed just have an old version of a plugin that has been updated. Keep an eye out for that.

If you’re wondering why to even bother with Rails 1.1, Scott Raymond currently has the best play-by-play overview of what’s new. We’ll be adding to that with more walkthroughs and hopefully movies around release time.

If you need more documentation, I strongly encourage you to pick up Chad Fowler’s Rails Recipe book. It’s currently out in its 3rd beta release and includes a bunch of great recipes on the new 1.1 features. Including RJS, polymorphic associations (and how to do better tagging with them), join models, integration testing, and more. You can get it as a PDF right now for $21.50.

So help us help you. Test Rails 1.1 with your existing applications. Try building new stuff with it. And let us know if something breaks in the process. We will be taking care of all heinous bugs before release. Thank you all!

Rails Training Coast to Coast (and beyond)

Mike and Dave are mobilizing their perpetually sold out Pragmatic Rails Studio again. This time, they’re hitting both coasts of the USA.

First in Portland on April 10 followed by Boston on May 11.

These session have sold out every time, so don’t miss your chance.

For those in Europe or the United Kingdom, Geoff Grosenbach will be doing a one-day Rails workshop in London on March 30 followed by me on April 10 with a four day workshop in London and Rails Core member Marcel Molina, Jr. with a five-day Rails Bootcamp in near Frankfurt, Germany.