In one of my previous articles about Ruby on Rails 2.x -> 3.0 changes I described all preparations that a Ruby on Rails 2.3x developer should consider, to make his Ruby on Rails 2.3x project RoR 3 oriented.
I promised that I’ll comment about deprecation warnings. So here we go:
First, make sure, you’re up to date with upcoming finder changes. Currently ActiveRecord provides the following finder methods:
• find(id_or_array_of_ids, options)
• find(:first, options)
• find(:all, options)
• first(options)
• all(options)
• update_all(updates, conditions, options)
and the following calculation methods:
• count(column, options)
• average(column, options)
• minimum(column, options)
• maximum(column, options)
• sum(column, options)
• calculate(operation, column, options)
In Rails 3, when you specify an option to any of these finders you’ll get deprecation warning. I believe that a piece of a code is better than a million loose words, so here is a quick cheatsheet with some comment:
# deprecated statement
Person.find(:all, :conditions => { :confirmed => true })
# a bit better version
Person.all(:conditions => { : confirmed => true })
# ‘you’re so COOL’ approach
named_scope :approved, :conditions => { : confirmed => true }
Person.approved.all
# ‘you’re so COOL’ again
Person.scoped(:conditions => { : confirmed => true }).all
You may go a little further than that and checkout fake_arel gem. I’ve already have written an article about it so you may browse our database if you want to find more information about this Ruby on Rails gem.
Second, don’t use constants like RAILS_ROOT, RAILS_ENV or RAILS_DEFAULT_LOGGER. Here are more up to date approaches: Rails.root, Rails.env, Rails.logger.
Third and last is about plugins. Rake tasks in vendor/plugins/PLUGIN/tasks are deprecated, as well the vendor/plugin/PLUGIN/rails/init.rb file, so you should lib/tasks directory and Railties instead. There may be some extra work needed with utilizing Railities, but you can get it to work with a legacy init.rb file.
Finally, I got good news. It is Monday now and all Ruby on Rails user know that Monday means new railscast with Ryan Bates! Due to some strange coincidence current railscast is dedicated to upgrading Ruby on Rails 2.x application to Ruby on Rails 3.0 compatibile. So give yourself a pleasure of watching it, I am sure Ryan will cover aspects discussed in this article better than me