2016-12-23 8 views
1

ActiveRecordを使用してこのようにするにはどうすればよいですか?ActiveRecordで時間間隔を比較するクエリ

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 

ありがとう:

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

開始仕上げは時間を記録しています。

答えて

2

あなたが使用できるのPostgresを使用している場合は、それの一つは、その後多くの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) 

if @reservationsWithinInterval1.count > 0 || @reservationsWithinInterval2.count>0 
flash[:notice] = 'Select different time. Car already reserved in this time' 
かどうかを確認します