Read more

Solving "TypeError (nil can't be coerced into Integer)" in the Rails console / IRB

Dominik Schöler
November 07, 2023Software engineer at makandra GmbH

On the Rails console, assigning an object to a variable can lead to this strange error (without stacktrace):

irb > recipient = Recipient.find(123)
Traceback (most recent call last):
TypeError (nil can't be coerced into Integer)
irb > recipient
#<Recipient ...
Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

The error is only in the output – the assignment is working. It only occurs when using the --nomultiline option, and thus only with IRB 1.2.0+ and before Ruby 3 Show archive.org snapshot . Also, it depends on the value of the assignment – not every Recipient (in this example) will trigger it.

Solution

Its cause is in the reline gem Show archive.org snapshot , and the issue is fixed in reline 0.2.0. So either gem install reline, or in projects with Bundler:

gem 'reline', '>= 0.2.0' # Fixes TypeError in Rails console
Posted by Dominik Schöler to makandra dev (2023-11-07 13:15)