Ruby code crashes with a ExceptionNoMethodError undefined method for nil:NilClass

66 views Asked by At

I know very little about Ruby, but inherited a Ruby app that has recently started to have an issue.

There's a Resque based job which runs periodically, and has been running fine for months and until now it suddenly stopped working. I verified the code hasn't changed, and thus thinking maybe it's a data related issue.

It runs for about a minute and then shows up as an error in Resque.

Retried just now 
Remove
ClassProcessor::ReportRunnerArguments
ExceptionNoMethodErrorErrorundefined method `nass_code' for nil:NilClass 

/var/www/applications/usps/app/models/report.rb:58:in `build_node'
/var/www/applications/usps/lib/processor/processor/reporter.rb:9:in `block in prep'

/usr/local/rvm/gems/ruby1.9.3p448/gems/activerecord3.2.8/lib/active_record/relation/delegation.rb:6:in `each'
/usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.8/lib/active_record/relation/delegation.rb:6:in `each'
/var/www/applications/usps/lib/processor/processor/reporter.rb:8:in `prep'
/var/www/applications/usps/lib/processor/processor/reporter.rb:15:in `execute'
/var/www/applications/usps/app/models/owner.rb:33:in `gone'
/var/www/applications/usps/lib/processor/processor/report_runner.rb:5:in `perform'

The line of code in question is:

<tran:NassCode>"+self.trip.master_destination.nass_code+"</tran:NassCode>

master_destination comes from app/models/trip.rb:

class Trip < ActiveRecord::Base

belongs_to :contract
belongs_to :origin, :class_name => 'Location'
belongs_to :destination, :class_name => 'Location'
belongs_to :frequency

...

def master_destination
   return master_trip_end.destination
end

And that Destination is a reference to a Location.rb, which has that 'nass_code' referenced in the error.

class Location < ActiveRecord::Base
  belongs_to :owner
  has_one :address, :as => :owner, :dependent => :destroy
  attr_accessible :name, :nass_code, :radius, :address_attributes

I know that's not much to go on - but any clues or ideas on what causes that kind of error, etc...

1

There are 1 answers

0
Mircea On

for at least one of your records master_destination is returning null.
I would add a debug statement in master_destination that just outputs the trip you are currently processing.
When it crashes, right before the crash you will see what trip has an issue.