I have a “job” model for adding job experiences to a resume and am
seeing the following error on occasion:
NoMethodError: undefined method `>’ for nil:NilClass
The line of code throwing the error is: if self.started_on >
self.ended_on
The code for this portion of the job form view is:
Date started *
Start: <%= f.date_select :started_on, :order =>
[:month, :year], :start_year=>1970, :include_blank=>true %>
Date ended *
Stop: <%= f.date_select :ended_on, :order =>
[:month, :year], :start_year=>1970, :include_blank=>true %>
Hint: leave blank for current position
The relevant code from the job model is:
def validate
unless self.ended_on.nil?
if self.started_on > self.ended_on
errors.add_to_base( “Start date is after end date. Please check
your dates.” )
end
end
end
In the form, it tells the user to select a start date and end date,
but to leave the end date blank if they are still employed at that
job. I am not certain why this is generating an error sometimes but
not others, other than some combination of selecting the start and
stop dates in an unexpected way.