Skip to content

Instantly share code, notes, and snippets.

@dominicsayers
Created March 12, 2015 13:07
Show Gist options
  • Save dominicsayers/e139682256b9f4ae61f8 to your computer and use it in GitHub Desktop.
Save dominicsayers/e139682256b9f4ae61f8 to your computer and use it in GitHub Desktop.
Upgrading from Rails 3.2 to Rails 4.2

I Googled an upgrade guide and read two or three before starting. Useful information.

This page just lists the things I didn't find in the upgrade guides.

Asset pipeline

  • Static files Rails 4.2 doesn't serve static files by default, and config.serve_static_assets is deprecated. Instead you need to set the environment variable RAILS_SERVE_STATIC_FILES to true in the production environment.

Rspec

I upgraded to Rspec 3.2. Things to note:

  • Stubbing a class you don't own can give unexpected results. I wanted to stub JSON.parse and force an exception but found that the begin...rescue around the code then didn't trap the exception. I moved the parsing to a separate method then stubbed the new method.
  • Routing: route_to expectations now seem to include the parameters passed to the route
  • Controller specs: You need to explicitly set the method when doing a call
  • Controller specs: With assigns expectations you can only match the whole instance variable, not components of it, e.g. expect(assigns(:avatar)['images'][0]['image']).to eq avatar doesn't work any more (I had to use the include matcher instead of eq)

ActiveRecord

  • Scopes need to be procs, e.g. scope :email, where(service: 'email') becomes scope :email, -> { where(service: 'email') }
  • You can't touch an object until it's persisted
  • find_or_create_by_... needs to be changed to equivalent first_or_create
  • Select/Pluck: This gives an error: SourceContact.all(select: :id). This is fine: SourceContact.all.pluck(:id)

ActiveSupport

  • Time.parse will only accept a string. You can't parse something that's already a date or time.

Routing

  • Resourceful routing syntax: get :company, to: :company gives a warning unless you change it to get :company, action: :company

Gems

I found one gem that didn't work with Rails 4, but I was deliberately using a very old version because the new one didn't do what I wanted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment