Upgrading an Old Rails Project with Codex

Introduction

Recently, I’m helping an old friend to upgrade their old Rails 4.2.8 + Ruby 2 project to Rail 5 and Ruby 3. Because it just cant run on new machines anymore.

Upgrading legacy Rails applications can be a daunting task. With numerous dependencies, outdated gems, and potentially deprecated code patterns, the process often feels like navigating through a maze. In this article, I’ll share my experience using Codex, an AI-powered tool, to assist in upgrading an old Rails project, making the process more manageable and efficient.

The Challenge

  • Hundreds of outdated gem dependencies
  • Legacy Ruby syntax that needed modernization
  • Deprecated Rails patterns and APIs

How Codex Helped

well, here is my conversations with codex and how it handled it.

Me: create a new branch based on this branch and upgrade this old project from ruby 2 to ruby 3 and upgrade rails from rails 4 to rails 6 while keep the project working.

Notes

    The test suite failed to run because Bundler couldn’t download a git-based gem dependency, as shown in the terminal logs

Summary

    Updated the Gemfile to require Ruby 3, Rails 6.1, and newer versions of key gems

Migrated application initialization to Rails 6 defaults and removed deprecated settings

Replaced deprecated serve_static_files with public_file_server.enabled in environment configs

Updated bin scripts for Rails 6 style loading of Spring

Reflected Rails 6 and Bundler 2.6.7 in Gemfile.lock

Testing

    ❌ bundle exec rake test (failed to load git-based dependency)

Codex couldn’t run certain commands due to environnment limitations. Consider configuring a setup script or internet access in your Codex environment to install dependencies.

ME: i changed my mind lets change to rails 5 instead of 6. and make sure bundle install command works.


Notes

    bundle install now succeeds after adjusting gem versions, but bundle exec rake test fails with a NoMethodError from Rake 11, indicating the test suite isn’t compatible with Ruby 3.

Summary

    Required Rails 5.2.8.1 and Ruby ~>3.0 with compatible gem versions like mysql2 and paperclip

Upgraded the wechat gem and disabled therubyracer for easier installation

Added a modern web-console version and pinned activerecord-session_store to ~>2.0

Initialized Rails defaults for 5.2 in the application config

Testing

    ✅ bundle install --jobs 4

❌ bundle exec rake test (fails with NoMethodError)

Me: Now, since we have upgraded to rails 5 from rails 4. Need the necessary code changes to make the project run ok.


Notes

The app now aliases removed *_filter callbacks to the Rails 5 *_action methods so older gems can hook into controllers. Key gems like authlogic, oauth2, faraday, and globalid were upgraded for Ruby 3 compatibility.

Summary

    Upgraded dependencies such as authlogic, rake, oauth2, and faraday to modern versions and pinned globalid to a compatible release

Added a legacy_filters initializer to provide *_filter aliases for gems expecting the old Rails callbacks

Updated Gemfile.lock to reflect the newer gem versions, including faraday 2.13 and oauth2 1.4.11

Testing

    ✅ bundle install --jobs 4

❌ bundle exec rake test (fails with SyntaxError in mime-types gem) don’t we have to update active record models and action controllers and migrations? since some classes have changed

Notes

    Added a new ApplicationRecord base class so all models inherit from it, matching Rails 5 conventions

Updated models to inherit from ApplicationRecord instead of ActiveRecord::Base, e.g. ShortLink

Specified migration version [5.2] for existing migrations to avoid compatibility warnings

Summary

  • Introduced ApplicationRecord and changed every model to inherit from it

  • Updated all migrations to declare ActiveRecord::Migration[5.2]

  • Fixed CKEditor asset model to use the new base class

Testing

    ❌ bundle install --jobs 4 (partial success before interruption)

❌ bundle exec rake test (Bundler failed to load command)

Codex couldn’t run certain commands due to environment limitations. Consider configuring a setup script or internet access in your Codex environment to install dependencies.

At this point i believe Codex has got the bundle install working on his environment. But a few gems were still not working on my local install.

  1. Authlogic, outdated, gonna replace with devise
  2. bybugs gem was a dev dependency added by another team member, i believe it is not required anymore
  3. mysql2 gem, this one is interesting. Will write the fix in next article.