sol.manager
2011-Jul-27 02:38 UTC
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: <label>Date started *</label> Start: <%= f.date_select :started_on, :order => [:month, :year], :start_year=>1970, :include_blank=>true %> <br/> <label>Date ended *</label> Stop: <%= f.date_select :ended_on, :order => [:month, :year], :start_year=>1970, :include_blank=>true %> <br> 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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2011-Jul-27 06:46 UTC
Re: help with a NoMethodError caused by comparing start and end dates
On 27 July 2011 03:38, sol.manager <sol.manager-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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_onThat means started_on is nil.> > > The code for this portion of the job form view is: > <label>Date started *</label> > Start: <%= f.date_select :started_on, :order => > [:month, :year], :start_year=>1970, :include_blank=>true %> > <br/> > <label>Date ended *</label> > Stop: <%= f.date_select :ended_on, :order => > [:month, :year], :start_year=>1970, :include_blank=>true %> > <br> > 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_onI 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.