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.
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 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
sumover nil elements- Missing DB default
- Operand order (
1 + nil)
⚡ Quick fix
- `(value
- 0) + 1
orvalue.to_i` - Add DB defaults and
null: false compact.sumfor arrays with nil
Detailed fix by platform
Ruby
total = items.map { |i| i.price.to_f }.sum
How to diagnose
- Operand — Which is nil?
- Source — Param/DB?
- 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:
Similar errors
- ArgumentError: missing keyword / unknown keyword Ruby: ArgumentError: missing keyword: :name / unknown keyword: :nmae
- ArgumentError: wrong number of arguments (given 1, expected 2) Ruby: ArgumentError: wrong number of arguments (given 1, expected 2..3)
- 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)
Most viewed in Ruby / Rails
- ActiveRecord::PendingMigrationError Rails: ActiveRecord::PendingMigrationError — Migrations are pending. To resolve this issue, run: bin/rails db:migrate
- ActionController::InvalidAuthenticityToken Rails: ActionController::InvalidAuthenticityToken (Can't verify CSRF token authenticity)
- ActionController::UrlGenerationError: No route matches {action...} missing required keys Rails: ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"users", :id=>nil}, missing required keys: [:id]
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 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.
Report a correction or suggest an improvement
Last updated 5 Nov 2026