ruby on rails - Validating time is not in the past without triggering error when a second has past after you selected the time -


i have validate on model:

def time_in_future   if start_at?     errors[:base] << 'the start date can not in past.' if start_at < time.zone.now   end end 

but every time try save error i'm asking in validation, makes sense. assume because time down second no matter when save, time automatically loads on datetimepicker going time in past because takes more second select time click "save".

i've thought of couple solutions, simplest of have error message read "the start date must time in future." easy enough. or, before saving, start_at time automatically saved time in future, thinking 15 minutes.

i assume must situation people regularly encounter, there better or more common way solve situation?

i appreciate input.

edit:

my updated method after taking input:

def time_in_future   if start_at?       errors[:base] << 'the start date must in future. seconds count!' if start_at < datetime.current + 15.minutes   end   end end 

another possible suggestion, i've implemented on large-scale production sites have "grace period." us, grace period 2 hours, short few seconds - depends on exact need.

so, code looked like:

def time_in_future   if start_at?     errors[:base] << 'the start date can not in past.' if start_at < time.current + 2.hours   end end 

the benefit of model 2.hours set per environment, make testing , troubleshooting other aspects of model involve start_at easier.


Comments