TypeError: nil can't be coerced into Integer Ruby / Rails Origin: Application & API Last updated 5 Nov 2026

Ruby: TypeError: nil can't be coerced into Integer / String can't be coerced into Integer

Arithmetic combined a number with nil (or a String) which Ruby can't convert automatically.

Exact message you may see TypeError: nil can't be coerced into IntegerTypeError (nil can't be coerced into Float)String can't be coerced into IntegerTypeError (String can't be coerced into Integer)nil can't be coerced into BigDecimal
Seen on: Ruby

Meaning

A variable or column used in +, *, sum is nil. Default values (to_i, || 0, .to_f), DB defaults (null: false, default: 0), and parsing params (Integer(params[:n], exception: false)) fix it. "5" + 1 gives "no implicit conversion", while 1 + "5" gives "String can't be coerced".

Common causes

  • nil attribute used in arithmetic
  • String from params not converted
  • sum over nil elements
  • Missing DB default
  • Operand order (1 + nil)

⚡ Quick fix

  1. `(value
  2. 0) + 1 or value.to_i`
  3. Add DB defaults and null: false
  4. compact.sum for arrays with nil

Detailed fix by platform

Ruby

  1. total = items.map { |i| i.price.to_f }.sum

How to diagnose

  1. Operand — Which is nil?
  2. Source — Param/DB?
  3. Default — Provided?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

ArgumentError: missing keyword / unknown keyword — Ruby: ArgumentError: missing keyword: :name / unknown keyword: :nmae (Ruby / Rails)ArgumentError: missin…ArgumentError: wrong number of arguments (given 1, expected 2) — Ruby: ArgumentError: wrong number of arguments (given 1, expected 2..3) (Ruby / Rails)ArgumentError: wrong …Bundler could not find compatible versions for gem — Bundler: Bundler could not find compatible versions for gem "activesupport": In Gemfile: rails (= 7.1.0) depends on activesupport (= 7.1.0) (Ruby / Rails)Bundler could not fin…Bundler: Could not find gem in locally installed gems — Bundler: Could not find gem 'rails (~> 7.1)' in locally installed gems / Could not find rails-7.1.0 in any of the gem sources listed in your Gemfile (Ruby / Rails)Bundler: Could not fi…FactoryBot::DuplicateDefinitionError: Factory already registered — FactoryBot: FactoryBot::DuplicateDefinitionError: Factory already registered: user (Ruby / Rails)FactoryBot::Duplicate…TypeError: no implicit conversion of nil into String — Ruby: TypeError: no implicit conversion of nil into String (Ruby / Rails)TypeError: no implici…undefined method for nil — Ruby: NoMethodError: undefined method `x' for nil (nil:NilClass) (Ruby / Rails)undefined method for …TypeError…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.