loading...
Cover image for Hiding Ruby 2.7 Deprecation Warnings in Rails 6

Hiding Ruby 2.7 Deprecation Warnings in Rails 6

andrewmcodes profile image Andrew Mason ・2 min read

Hiding Ruby 2.7 Deprecation Warnings in Rails 6

If you have upgraded your Rails app to Ruby 2.7, you are probably seeing a lot of deprecation messages in your console. You should first make sure that none of these messages are coming from your code, and address them if they are! 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.

Here are some options you have to make that happen. But first, lets create a new Rails app to experiment with!

Generate a new Rails app with Ruby 2.7

You can either use the CLI or this template.

If you use the CLI, I recommend something like:

rails new silence_ruby_2_7_deprecations -d postgresql --webpack=stimulus
Enter fullscreen mode Exit fullscreen mode

Method #1: Using dotenv-rails

If you are using the dotenv-rails gem, or another method of using .env files, simply add the following to your .env file:

export RUBYOPT='-W:no-deprecated -W:no-experimental'
Enter fullscreen mode Exit fullscreen mode

Then run the following in the root of the project:

source .env
Enter fullscreen mode Exit fullscreen mode

You should no longer be seeing the Ruby 2.7 deprecation warnings coming out of Rails! πŸŽ‰

Method #2: Prefixing commands

Another option you have for ignoring the Ruby 2.7 deprecation warnings is to prefix all of your Rails commands with RUBYOPT='-W:no-deprecated -W:no-experimental'.

Example:

  • rails server would become RUBYOPT='-W:no-deprecated -W:no-experimental' rails server
  • rails console would become RUBYOPT='-W:no-deprecated -W:no-experimental' rails console
  • etc.

This is obviously not ideal but it will work!

Method #3: Updating your environment

If you want to disable these deprecation messages everywhere, you can add the following to your ~/.zshrc or ~/.bashrc:

export RUBYOPT='-W:no-deprecated -W:no-experimental'

This will disable deprecation and experimental feature warnings for all versions of Ruby, for all projects. I have heard of this creating issues for some so you may want to be careful using this method if you work on multiple apps that aren't on Ruby 2.7.

View repo for this post

Hopefully this helps! Happy coding!

Discussion

pic
Editor guide
Collapse
woto profile image
Ruslan Kornev

Hi Andrew, i've cloned your repository and still got warnings. What I'm doing wrong?

[1] pry(main)> Post.first
/Users/r.kornev/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/activemodel-6.0.2.1/lib/active_model/type/integer.rb:13: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/Users/r.kornev/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/activemodel-6.0.2.1/lib/active_model/type/value.rb:8: warning: The called method `initialize' is defined here
/Users/r.kornev/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/activerecord-6.0.2.1/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb:12: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/Users/r.kornev/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/activemodel-6.0.2.1/lib/active_model/type/value.rb:8: warning: The called method `initialize' is defined here
  Post Load (1.0ms)  SELECT "posts".* FROM "posts" ORDER BY "posts"."id" ASC LIMIT $1  [["LIMIT", 1]]
=> nil
Collapse
michelegera profile image
Michele Gerarduzzi

On method #1 (using dotenv-rails), the line to add to .env should read:

RUBYOPT='-W:no-deprecated -W:no-experimental'

as the dotenv-rails gem will take care to export it.

Collapse
waynehoover profile image
Wayne

None of these worked for me.

Collapse
andrewmcodes profile image
Andrew Mason Author

Did you reload your zshrc/bashrc or reload your terminal?

Collapse
leesmith profile image
Lee Smith 🍻

So this is just to silence them within ruby? Won’t rails internals eventually knock out these warnings?

Collapse
andrewmcodes profile image
Andrew Mason Author

They will but this will help until that update lands!