Deprecation warnings in Rails 5
Asked Answered
M

2

7

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?

Maltz answered 6/10, 2016 at 13:10 Comment(6)
It is probably being used inside a gem, take a look at all of your gems and see if any of them are using it.Kaseykasha
+1 Doon, Have you done a "bundle update" recently to ensure you have the latest gems. Many gems as of yet don't officially support rails 5Marianamariand
All gems are updated after upgrading to Rails 5. How can you detect from which gem it can be coming?Maltz
you can grep through your gem repositoryMarianamariand
You can also safely ignore it, as your gem dependency grows, it is virtually impossible to keep your warning list clear.Marianamariand
Ignoring these warnings is not a good idea. I've worked on applications that have made their way from Rails 2 - 5 without any warnings.Diagnose
D
4

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 :)

Diagnose answered 15/6, 2017 at 22:49 Comment(0)
A
0

I ran into this same error recently. Got this error about 2 minutes into creating a new Rails 5 application. I had barely even started the following chartkick tutorial when I got same error.

Order of Operation

  1. rails g model Visit country:string visited_at:datetime load_time:decimal
  2. rake db:migrate
  3. BOOM! received same error & it would not let me continue making my schema.

The Error: 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 richOnRails/chartkick/config/application.rb:7) rake aborted!

The error message mentioned trouble in line 7 of config/application.rb depicted below:

require_relative 'boot'
require 'rails/all'

**Bundler.require(*Rails.groups)**
module Chartkick
    class Application < Rails::Application
    end
end

Line 7: Bundler.require(*Rails.groups) *asterisks used to distinguish line 7.

I didn't see anything truly wrong with line 7.


Tried to solve it with following commands:

  • bundle exec rake db:migrate [got same error]

  • bundle update

Finally, solved part of the issue giving me the error - with the following code:

bin/rake db:create

rake db:migrate

I still have the same error message but I'm now able to use & access my database. The error apparently is still present but it's no longer hindering my progress. I should also note that my new rails app was built with postgresql as default database.

rails new chartkick --database=postgresql

Not sure if this is helpful in your case but it might help others in debugging this problem.

Avert answered 22/1, 2017 at 23:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.