3.times { print "Ruby! " }
Upgrading to Rails 3 Beta

Today I attempted to upgrade a Rails app from 2.3.5 to 3.0.beta. I assumed this might be a monumental undertaking, and was prepared to cut things short if needed.

The bad news is that I did stop half way through, and have reverted to 2.3.5. The good news is the basic upgrade path was easier than I expected.

There’s now an official upgrade plugin which you can install into your existing Rails 2.x app that provides a number of rake tasks that help you determine what’s going to change, and provide code so you can make it happen:

http://github.com/rails/rails_upgrade

Jeremy McAnally, the creator of the plugin, also has a nice tutorial on getting Rails 3 beta installed on your system:

http://omgbloglol.com/post/371893012/the-path-to-rails-3-greenfielding-new-apps-with-the

Once Rails 3 is on your system, and you’re ready for the upgrade, it’s as simple as doing

$ cd myapp
$ rails . 

After updating all my config files with proper database settings and the like, I fired up the server and found to my delight that everything seemed to work, including my Metal classes.

I then moved on to getting my RSpec testing suite working, and that’s when I ran into problems. After tinkering around for a while attempting to install the alpha of RSpec 2, I realized that rspec and rspec-rails are a bit far off from being ready for Rails 3. Since testing is intregal to my development, it was a no go for an upgrade. :(

So it seems that once the community has time to update gems and plugins for Rails 3, things will be looking good. Personally, I can’t wait to take advantage of all the Rails 3 has to offer.

nil?, empty? and blank?

rubyquicktips:

In Ruby, you check with nil? if an object is nil:

article = nil
article.nil?  # => true

empty? checks if an element - like a string or an array f.e. - is empty:

# Array
[].empty?   #=> true
# String
"".empty?   #=> true

Rails adds the method blank? to the Object class:

An object is blank if it‘s false, empty, or a whitespace string. For example, “”, ” “, nil, [], and {} are blank.

This simplifies

if !address.nil? && !address.empty?

to

if !address.blank?

Documentation: nil?, empty? (String, Array) and blank?

As of Rails 2.3, there’s also a handy new try() method on objects, which allows you to invoke a method on a possibly nil object without throwing a NoMethodError. This saves you the trouble of checking if your object is nil or not before accessing a method.

For example, previously you’d need to do something like

article = Article.find_by_title("My Article")
unless article.nil? 
  article.body
end

With try() you can skip the nil? check and do the following

Article.find_by_title("My Article").try(:body) => #body or nil