Help with a NoMethodError caused by comparing start and end dates

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.

On 27 July 2011 03:38, sol.manager [email protected] wrote:

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

That means started_on is nil.


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

I guess you have the situation where ended_on is not nil but
started_on is. If you don’t think this should be happening then have
a look at the Rails Guide on debugging to find out how to use
ruby-debug to break into the code and inspect data.

Colin