Preparing for Rails 2.0

Anyone that has used Rails 1.2.3, 1.2.4 or 1.2.5 may have noticed a number of deprecation notices in their development logs. Whilst these deprecated methods still work as expected in 1.2.x versions, you will come-a-cropper when you try to upgrade to Rails 2.0. So what do you need to do and what tools are out there to help you with the move? Glad you asked.

The first thing you can do it run your code through a code checker — Geoffrey Grosenbach has released a great rake task which digs through your code looking for the old methods. It will give you hints of how to fix the issues, but lets look at them a little more closely.

@params, @session, @flash, @env

As of Rails 2.0, you won’t be able to directly access the above instance variables. They have been replaced with methods, which makes customising their actions much easier. It also allows the internals of Rails to change without breaking the API. This is very easy to fix – just remove the @ in front of those variables – they will work exactly the same.

find_all, find_first, render_partial

In earlier version of Rails there were a number of grouped methods, that do very similar things – find, find_all and find_first all fetch records from the database, the only difference is the number of records they return. It was decided to combine these methods in to one where they are differentiated by passed in options. So find_all becomes find(:all) and find_first becomes find(:first) and render_partial becomes render(:partial).

Forms

Out of all the HTML helpers, the form tag was an anomaly because it required a start AND end helper. To make it fit in with way the rest of Rails works and to facilitate dynamic form generation, a block method called form_tag was created. This particular update has a trap in it through – because blocks don’t return values, the ERB tag you use must not have an = sign, so


   <%= start_form_tag %>
       <!-- Form stuff -->
   <%= end_form_tag %>

becomes


    <% form_tag do %>
        <!-- Form stuff -->
    <% end %>

Notice the omission of the equals sign in the latter example?

Also note that passing :post => %gt; true is deprecated. With the push for RESTfulness, the form needs to know about the other HTTP verbs, put and delete, so a new option has been created:


    <% form_tag :method => :post do %>
        <!-- Form stuff -->
    <% end %>

Plugins

A number of what used to be core components of rails have been moved out into plug-ins so as not to clutter the core with stuff that you don’t use very often. It also means that the development of the plugins can be much quicker than that of the core. Probably the major extraction is the third-party database interfaces. Now, by default only MySQL, SQLite and PostgreSQL are supported out of the box. All other databases are supported via gems named activerecord-database-adapter. If you want to use an Oracle just run

gem activerecord-oracle-adapter

and you will be peachy again.

Other extractions of note are the acts_as plug-ins. If you use acts_as_tree or acts_as_list in your model, you will need to script/plugin install them and the built-in pagination has now become the classic_pagination plug-in. Note that by the developers own admission that plug-in is slow (and was slow when it was in core), so if you use it, you may want to think about migrating across to the new and improved will_paginate plug-in.

So hop to it and get your web apps upgraded now before the rush!

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • http://www.soakedandsoaped.com/ kommen

    There is a little script which checks existing rails for a rough rails 2.0 compatibility:
    http://pastie.caboo.se/private/krcevozww61drdeza13e3a

  • http://www.twice21.com ParkinT

    Wow. After a slow start, Rails seems to be ‘gathering steam’!
    I think this is, in large part, due to the recent publications Sitepoint has provided; permitting more developers to get their hands dirty with Ruby on Rails.
    I am having fun learning a new language (adding to my current collection of over a dozen) and applying the many lessons offered here.

  • yougottabekidding

    A slow start!? Rails is going like gangbusters, and has for a couple of years.

    ParkinT has it completely backwards. It is not Rails that has benefited from SitePoint, but SitePoint that has benefited from Rails, capitalizing on its popularity by publishing books. Rails would be just fine (thank you) without SitePoint. Not to say the Sitepoint books aren’t good, but they cannot in a million years take credit for Rails’ success.

  • MarcelloDL

    render :partial seems not assuming the calling controller anymore, so:

    render :partial => ‘/partial’

    won’t work.

    render :partial => ‘controller_name/partial’

    will instead pick the partial named “_partial” in app/views/controller_name

  • AkitaOnRails

    I invite everybody to take a look at a screencast I’ve compiled yesterday. This is the classic Blog app built using Rails 2.0. I think it is the First Rails 2.0 full featured screencast around.

    See it “here”:http://www.akitaonrails.com/2007/12/10/the-first-rails-2-0-screencast-english