Validating start and end time attributes

1.5k Views Asked by At

I am trying to validate what I thought would be fairly simple to do. I have two time_select attributes "start_time" and "end_time". I am simply trying to validate in the controller whether the end time entered by the user is equal to the start time or before the start time. There are no dates involved at all. I have a validation method below however it always errors no matter what is entered by the user.

Fairly new to rails and can't help but feel im missing something small. This validation should work shouldn't it?

class Casenote < ActiveRecord::Base
 validate :end_must_be_after_start

 belongs_to :servicelog
 default_scope { order(:id) }


 private

 def end_must_be_after_start
   if :start_time >= :end_time
     errors.add(:end_time, "must be after start time")
   end
 end

end
1

There are 1 best solutions below

7
Kristján On

You are comparing the two symbols :start_time and :end_time. You need to be comparing the two attributes start_time and end_time.

def end_must_be_after_start
  if start_time >= end_time
    errors.add(:end_time, "must be after start time")
  end
end