Reputation: 128
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.
Upvotes: 1
Views: 1439
Reputation: 141
@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'
Upvotes: 0
Reputation: 14890
If you're using Postgres you can use one of it's many time and date functions
Reservation.where("(finish - interval '1 hour') < start")
Upvotes: 2