How to validate overlapping times in Rails

前端 未结 4 1350
渐次进展
渐次进展 2021-01-20 07:08

I have an Event model that has form time and to time in my schedule app and I want to validate the overlapping time before saving.

4条回答
  •  Happy的楠姐
    2021-01-20 07:28

    In your EventsController#Create & EventsController#Update methods check for new time in between the old times like :

    events = Event.where(:departure_date => event_params[:departure_date])
    events.each do |event|
     if event_params[:to].between?(event.to, event.from) || event_params[:from].between?(event.to, event.from)
      flash[:error] = "Current Slot Time is already taken!"
      redirect_to :back
      return
     end
    end
    

提交回复
热议问题