Upgrading our RoR app to Rails 3

Published 14 October 2010

I’m running Ubuntu 10.10 with an existing Rails application on version 2.3.5 and a bunch of Rspec tests.\r\n\r\nWriting this from memory so let me know if I’ve missed any steps out.\r\n\r\nThis is what the environment.rb file looks like:\r\n\r\n@@@ ruby\r\n config.gem \“mysql\”, :version => \“2.8.1\”\r\n config.gem \“haml\”, :version => \“=2.2.17\”\r\n config.gem \“compass\”, :source => \“http://gemcutter.org/\”, :version => \“=0.10.0.rc1\”\r\n config.gem \“compass-colors\”, :version => \“=0.3.1\”\r\n config.gem \“fancy-buttons\”, :version => \“=0.4.1\”\r\n config.gem ‘whenever’, :lib => false, :source => ‘http://gemcutter.org/’, :version => \“=0.4.2\”\r\n config.gem \“validation_reflection\”, :source => \“http://gemcutter.org\”, :version => \“=0.3.5\”\r\n config.gem ‘formtastic’, :version => \“=1.1.0\”\r\n config.gem \“RedCloth\”, :version => \“=4.2.2\”\r\n config.gem \“paperclip\”, :version => \“=\”\r\n config.gem \“acts_as_audited\”, :version => \“=1.1.1\”\r\n config.gem \“spreadsheet\”, :version => \“=\” # for exporting to excel spreadsheets\r\n config.gem \“fastercsv\”, :version => \“=1.5.3\” #for exporting to csv\r\n config.gem \“authlogic\”, :version => \“=2.1.5\”\r\n config.gem \“thinking-sphinx\”, :lib => \“thinking_sphinx\”, :version => \“1.3.16\”\r\n config.gem ‘delayed_job’, :version => \“=1.8.5\”\r\n config.gem ‘ts-delayed-delta’, :lib => ‘thinking_sphinx/deltas/delayed_delta’, :version => ‘= 1.0.4’, :source => ‘http://gemcutter.org’\r\n config.gem \“tmail\”, :version => \“=\” # for receiving and parsing emails\r\n config.gem ‘newrelic_rpm’, :version => ‘=2.12.1’\r\n config.gem ‘will_paginate’, :version => ‘=2.3.11’, :source => ‘http://gemcutter.org’\r\n config.gem \“crewait\”, :version => ‘=0.1.0’ # provides the ability to bulk-insert multiple rows\r\n config.gem \“memcache-client\”, :version => ‘=1.8.3’\r\n config.gem \“system_timer\”, :version => ‘=1.0’ # memcache-client uses this\r\n config.gem \“daemons\”, :version => ‘=1.0.10’ # allow inbound email processor to run as a daemon\r\n config.gem \“populator\”, :version => \“=0.2.5\” # populate the demo account\r\n config.gem \“faker\”, :version => \“=0.3.1\” # populate the demo account\r\n@@@\r\n\r\nh2. Take a backup\r\n\r\nEither make sure your source control is up-to-date, or just take a complete directory copy to myapp-rails2. You’ll be checking if you’ve made customisations to files later so having a side-by-side comparison is handy.\r\n\r\nh2. Check gems\r\n\r\nI needed to check all my gems are working with Rails 3. \This site\ is great for finding the success others have had.\r\n\r\nFYI some gems still don’t work with Rails 3 so this may be a show stopper.\r\n\r\nh2. Install the Rails Upgrade plugin\r\n\r\nThis plugin \http://github.com/rails/rails_upgrade\ lists the changes you need to make, tells you what’s deprecated in your code and generates your new application.rb and routes files.\r\n\r\nOnce installed generate your new routes file (this will overwrite your routes file):\r\n\r\n@@@\r\nrake rails:upgrade:routes > config/routes.rb\r\n@@@\r\n\r\nh2. Install Bundle\r\n\r\nI installed bundle as a regular user, not sudo:\r\n\r\n@@@\r\ngem install bundler\r\n@@@\r\n\r\nand generate the Gemfile using the Rails Upgrade plugin command:\r\n\r\n@@@\r\nrake rails:upgrade:gems > Gemfile\r\n@@@\r\n\r\nI had to tweak some of my gems to force them to use some prerelease and release candidate versions to work with Rails 3. This is an iterative process finding gems which don’t work.\r\nFor instance thinking-sphinx required version 2.0.0.rc2, delayed_job required 2.1.0.pre2, will_paginate required 3.0.pre2.\r\n\r\nI also had to force the whenever gem to upgrade to version 0.5.3.\r\n\r\nh2. Use Bundle to install all your gems\r\n\r\n@@@\r\nbundle install\r\n@@@\r\n\r\ninstalls all the gems required for the project. This takes a while as it’s installing Rails 3 and all associated gems too.\r\n\r\nAs I found older gem versions which didn’t work with Rails 3 I set the pre, beta or rc version in the Gemfile then ran\r\n\r\n@@@\r\nbundle update\r\n@@@\r\n\r\nwhich checks installed gems and gets the latest versions.\r\n\r\nh2. Run rails to migrate the application\r\n\r\nInside your application RAILS_ROOT directory run \r\n\r\n@@@\r\nrails new .\r\n@@@\r\n\r\nThis essentially tries to create a new Rails 3 application over the top of your application. This is why you’ve got that backup from step 1, right?\r\n\r\nIt prompts you to overwrite or skip your existing files.\r\n\r\nSo for your files where you’ve made customisations (routes.rb and Gemfile) for instance, choose ‘n’ to skip. For other files where you’re not sure it’s easier to say yes then merge your old files if necessary.\r\nChoose yes to overwrite your environment.rb file since all the gem directives are unused now.\r\n\r\nh2. Modify your code\r\n\r\nI had millions of deprecation warnings and a bunch of exceptions which I had to fix up.\r\n\r\nOne of them was using Formtastic with authlogic. I had to modify my user_session.rb model file with an include and an extra method:\r\n\r\n@@@ ruby\r\nclass UserSession < Authlogic::Session::Base\r\n include ActiveModel::Conversion # added this\r\n \r\n # and added this method\r\n def persisted?\r\n false\r\n end\r\nend\r\n@@@\r\n\r\nFormtastic played nice with Authlogic after that. You may be able to skip this step as I understand there’s a fix coming out for this in the next Authlogic version.\r\n\r\nh2. Removed deprecated code\r\n\r\nWherever there’s a RAILS_ROOT or RAILS_ENV it now needs to be replaced with Rails.root and Rails.env. Well almost_, they’re a bit smarter than just strings as this short blog illustrates \http://litanyagainstfear.com/blog/2010/02/03/the-rails-module/\ also removed all my before_validation_oncreate method calls which are also deprecated. The Rails Upgrade plugin tells you all this too.\r\n\r\nh2. Haml and Sass\r\n\r\nI got a many Sass deprecation warnings, just about removing the .sass extension from imports\r\n\r\nWe use Haml instead of erb so I had to delete the automatically generated application.erb layout file.\r\n\r\nh2. New reserved words\r\n\r\nAs always seems to be the case in Rails until you get a bit savvy, there's a bunch of undocumented reserved words which cause curious errors and result in keyboard-forehead mashing.\r\n\r\nI had a model with a field called 'field' which is a no-no now. But the error I was getting was\r\n\r\n@\r\n field_changed? is defined by ActiveRecord\r\n@@\r\n\r\nand an awesome exception called ActiveRecord::DangerousAttributeError, which is, of course, my middle name.\r\n\r\nh2. Rspec pain\r\n\r\nOK, you know what? While writing the blog post there was so much I had to do to get my tests to pass again, it’s worthy of a completely separate blog post.\r\n\r\nSo that’s coming up next.\r\n\r\nUPDATE: \Here’s the blog on getting Rspec 2 working\. Working!?\r\n\r\nSo aside from more than half my tests failing, and getting thousands of failing tests, the site is working now.\r\n\r\nI had to disable ts-delayed-delta (which is the mechanism we use for keeping the thinking sphinx search up-to-date, you probably don’t need this for your app), so I will need to find a work-around solution for this.\r\n\r\nApart from that the site appears to be all working on shiny Rails 3.\r\n\r\nPlease leave a comment with your experiences