Undefined method `+' for nil:NilClass in Ruby

I am using RubyMine to create a website. I have a calculation I’m doing
in the model that is no longer working. It has worked previously, but
now I’m receiving a NoMethodError stating that there is an undefined
method `+’ for nil:NilClass

I have multiple integers I’m adding in my “roster.rb” model, with the
scope sorting as follows:

scope :sorted, -> { select('*, (passing_yards + passing_tds +

qb_rushing + coaching_staff + age + physical_tools + intellect +
surrounding_talent + health + consistency) as xoi’).order(‘xoi DESC’) }

def xoi
    passing_yards + passing_tds + qb_rushing + coaching_staff + age
  • physical_tools + intellect + surrounding_talent + health + consistency

I have database entries using these integers that have worked just days
ago, with the calculation working properly, and the numbers sorting

I can still define “xoi” with just one of these integers and the integer
will show up properly. I can also define “xoi” with 2 + 2 and the answer
will show up on my webpage properly as 4. But this calculation is no
longer working.

The html page displaying the results of this calculation is as follows:

 <% @players.each do |player| %>
          <td><%= player.xoi %></td>
          <td><%= player.last_name %></td>
          <td><%= player.first_name %></td>
          <td><%= player.pos %></td>
          <td><%= player.team %></td>
          <td><%= player.passing_yards %></td>
          <td><%= player.passing_tds %></td>
          <td><%= player.qb_rushing %></td>
          <td><%= player.coaching_staff %></td>
          <td><%= player.age %></td>
          <td><%= player.physical_tools %></td>
          <td><%= player.intellect %></td>
          <td><%= player.surrounding_talent %></td>
          <td><%= player.health %></td>
          <td><%= player.consistency %></td>

I must have made some small tweak that I can’t recover from. Version
Control did not work for me.

It’s as though the “+” is just no longer working for this calculation
(but I’m sure I’m just doing something wrong).

I’d appreciate any insight.

Thanks Jesus.

I’m new to Ruby though, and don’t know how to do this. I did a search
about setting Default Values and came up with a lot of confusing

Do you know an easy way to do this?

Thank you so much for your help.


Jesus Castello wrote in post #1172638:

One of the variables you are using in the calculation is nil for some
reason, and that’s why your code is trying to call the ‘+’ method on

You should check for nil or make sure that the value can’t be nil (for
example, by setting a default value).

So you either do this:

def xoi
if passing_yards && passing_tds && coaching_staff …
# Do calculation

Or even better, set a default at the database level so a value can never
be undefined.

Charles D. wrote in post #1172765:

Do you know an easy way to do this?

You can change:
def xoi
passing_yards + passing_tds + qb_rushing + coaching_staff +
age+physical_tools+intellect+surrounding_talent+health +consistency

by this :

def xoi
sum+= passing_yards || 0
sum+= passing_tds|| 0
sum+= qb_rushing|| 0
sum+= coaching_staff|| 0
sum+= age|| 0
sum+= physical_tools|| 0
sum+= intellect|| 0
sum+= surrounding_talent|| 0
sum+= health|| 0
sum+= consistency|| 0
return sum


Thanks so much Jesus. I really appreciate it.