1

How to do something like this using ActiveRecord?

Reservation.where("start..finish < 1 hour")

start and finish are time records:

2.2.1 :009 > Reservation.last.start.class
  Reservation Load (1.1ms)  SELECT  "reservations".* FROM "reservations"  ORDER BY "reservations"."id" DESC LIMIT 1
  Reservation Load (1.1ms)  SELECT  "reservations".* FROM "reservations"  ORDER BY "reservations"."id" DESC LIMIT 1
 => ActiveSupport::TimeWithZone 

Thanks.

0

2 Answers 2

2

If you're using Postgres you can use one of it's many time and date functions

Reservation.where("(finish - interval '1 hour') < start")
0
@reservationsWithinInterval1 = Reservation.where('startdate >= ? AND startdate < ? , @reservation.startdate, @reservation.enddate)

@reservationsWithinInterval2 = Reservation.where('enddate >= ? AND enddate < ?, @reservation.startdate, @reservation.enddate)

Then check if

if @reservationsWithinInterval1.count > 0 || @reservationsWithinInterval2.count>0
flash[:notice] = 'Select different time. Car already reserved in this time'

Not the answer you're looking for? Browse other questions tagged or ask your own question.