Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deprecation warnings in Rails 5

Every time I execute my tests, I get these deprecation warnings:

DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from <top (required)> at /Users/johnvanarkelen/Documents/Web development/rails/test-eagle/config/environment.rb:5)
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from <top (required)> at /Users/johnvanarkelen/Documents/Web development/rails/test-eagle/config/environment.rb:5)
DEPRECATION WARNING: after_filter is deprecated and will be removed in Rails 5.1. Use after_action instead. (called from <top (required)> at /Users/johnvanarkelen/Documents/Web development/rails/test-eagle/config/environment.rb:5)

When I check line 5 of config/environment.rb, there is this code:

Rails.application.initialize!

When I search my repo for after_action, after_filter or alias_method_chain, it is not found. What can I do to get rid of these warnings?

like image 494
John Avatar asked Oct 06 '16 13:10

John


People also ask

How do you fix a deprecation warning?

To fix all deprecation warnings, follow the below steps: Replace update() with updateOne() , updateMany() , or replaceOne() Replace remove() with deleteOne() or deleteMany() . Replace count() with countDocuments() , unless you want to count how many documents are in the whole collection (no filter).

What are deprecation warnings?

Deprecation warnings are a common thing in our industry. They are warnings that notify us that a specific feature (e.g. a method) will be removed soon (usually in the next minor or major version) and should be replaced with something else.

How do you add a deprecation warning?

To warn about deprecation, you need to set Python's builtin DeprecationWarning as category. To let the warning refer to the caller, so you know exactly where you use deprecated code, you have to set stacklevel=2 .

How do you get rid of warnings in Ruby?

If the deprecations are coming mostly from Rails, it may be time to disable the messages and save yourself from messy terminal output. The TL;DR is that you need to use RUBYOPT='-W:no-deprecated -W:no-experimental' to disable the deprecations. This will also disable experimental feature warnings as well.


1 Answers

Why

I ran into alias_method_chain is deprecated ... from <top (required)> at /path/to/your/environment.rb in a recent rails 5 upgrade. This typically points to usage in a gem required during the initialize! call on your Rails application or a Bundler.require if you are manually requiring your dependencies (which I happened to be).

It's probably better expressed as:

One of the random Gems you depend on did something I didn't like while Rails was initializing. Have fun finding it!

How to fix it (maybe)

These are the loose steps I followed to sort out these unknown errors:

  1. Find you gem path
    • You can typically use the parent directory of bundle show rails or $GEM_PATH to figure out where the gems for your bundle currently live (do note that this may be different for gems from different sources, e.g. git dependencies are stored differently than those from rubygems)
  2. Use your favorite text search tool (grep, ripgrip ag) to search for the deprecated method name e.g. rg "alias_method_chain" <gem path>
  3. Upgrade or remove that gem to see if the deprecation goes away
  4. Hopefully it does!

In my case, the deprecation warning for alias_method_chain was in an outdated version of Kaminari. I updated my Gemfile to allow for a more recent version and then ran bundle update kaminari to sort it.

An Aside

Ignoring these warnings is a very bad idea; it's easy to let little things like this slip into your application if they have no immediate impact. That leads to normalizing the behavior of ignoring warnings or errors; that means more bugs and inconsistency in your application and a guaranteed bad time when you try to upgrade to a version of rails that removes the deprecated behavior. If you or your team establishes a standard of finding and fixing these errors immediately it's easy to spot and resolve real problems when they come up. Your future self will thank you :)

like image 108
Nick Tomlin Avatar answered Sep 21 '22 15:09

Nick Tomlin