Ruby on rails 使用postgresql在rails中的daterange中查询的位置

Ruby on rails 使用postgresql在rails中的daterange中查询的位置,ruby-on-rails,postgresql,date-range,Ruby On Rails,Postgresql,Date Range,如何在带有Rails 4.2的postgresql中使用带有日期范围的where查询: ConsultingLocation Load (0.6ms) SELECT "consulting_locations".* FROM "consulting_locations" WHERE "consulting_locations"."deleted_at" IS NULL AND "consulting_locations"."id" = $1 LIMIT 1 [["id", 495]]

如何在带有Rails 4.2的postgresql中使用带有日期范围的where查询:

  ConsultingLocation Load (0.6ms)  SELECT  "consulting_locations".* FROM "consulting_locations" WHERE "consulting_locations"."deleted_at" IS NULL AND "consulting_locations"."id" = $1 LIMIT 1  [["id", 495]]
  ConsultingLocationDoctorSchedule Load (1.7ms)  SELECT "consulting_location_doctor_schedules".* FROM "consulting_location_doctor_schedules" INNER JOIN "consulting_location_doctors" ON "consulting_location_doctor_schedules"."consulting_location_doctor_id" = "consulting_location_doctors"."id" WHERE "consulting_location_doctors"."deleted_at" IS NULL AND "consulting_location_doctors"."consulting_location_id" = $1  [["consulting_location_id", 495]]
=> [#<ConsultingLocationDoctorSchedule:0x007fe7653f7538
  id: 1,
  consulting_location_doctor_id: 495,
  schedule_date: Mon, 05 Jan 2015 00:00:00 IST +05:30,
  slot_details:
   [{"end"=>"2015-01-05T03:00:00.000+00:00", "start"=>"2015-01-05T02:30:00.000+00:00", "title"=>" 2:30 am to  3:00 am", "appointment_id"=>""},
    {"end"=>"2015-01-05T03:30:00.000+00:00", "start"=>"2015-01-05T03:00:00.000+00:00", "title"=>" 3:00 am to  3:30 am", "appointment_id"=>""},
    {"end"=>"2015-01-05T04:00:00.000+00:00", "start"=>"2015-01-05T03:30:00.000+00:00", "title"=>" 3:30 am to  4:00 am", "appointment_id"=>""},
    {"end"=>"2015-01-05T04:30:00.000+00:00", "start"=>"2015-01-05T04:00:00.000+00:00", "title"=>" 4:00 am to  4:30 am", "appointment_id"=>""},
    {"end"=>"2015-01-05T05:00:00.000+00:00", "start"=>"2015-01-05T04:30:00.000+00:00", "title"=>" 4:30 am to  5:00 am", "appointment_id"=>""},
    {"end"=>"2015-01-05T05:30:00.000+00:00", "start"=>"2015-01-05T05:00:00.000+00:00", "title"=>" 5:00 am to  5:30 am", "appointment_id"=>""},
    {"end"=>"2015-01-05T06:00:00.000+00:00", "start"=>"2015-01-05T05:30:00.000+00:00", "title"=>" 5:30 am to  6:00 am", "appointment_id"=>""},
    {"end"=>"2015-01-05T06:30:00.000+00:00", "start"=>"2015-01-05T06:00:00.000+00:00", "title"=>" 6:00 am to  6:30 am", "appointment_id"=>""},
    {"end"=>"2015-01-05T07:00:00.000+00:00", "start"=>"2015-01-05T06:30:00.000+00:00", "title"=>" 6:30 am to  7:00 am", "appointment_id"=>""},
    {"end"=>"2015-01-05T07:30:00.000+00:00", "start"=>"2015-01-05T07:00:00.000+00:00", "title"=>" 7:00 am to  7:30 am", "appointment_id"=>""},
    {"end"=>"2015-01-05T08:00:00.000+00:00", "start"=>"2015-01-05T07:30:00.000+00:00", "title"=>" 7:30 am to  8:00 am", "appointment_id"=>""},
    {"end"=>"2015-01-05T08:30:00.000+00:00", "start"=>"2015-01-05T08:00:00.000+00:00", "title"=>" 8:00 am to  8:30 am", "appointment_id"=>""},
    {"end"=>"2015-01-05T09:00:00.000+00:00", "start"=>"2015-01-05T08:30:00.000+00:00", "title"=>" 8:30 am to  9:00 am", "appointment_id"=>""},
    {"end"=>"2015-01-05T09:30:00.000+00:00", "start"=>"2015-01-05T09:00:00.000+00:00", "title"=>" 9:00 am to  9:30 am", "appointment_id"=>""},
    {"end"=>"2015-01-05T10:00:00.000+00:00", "start"=>"2015-01-05T09:30:00.000+00:00", "title"=>" 9:30 am to 10:00 am", "appointment_id"=>""},
    {"end"=>"2015-01-05T10:30:00.000+00:00", "start"=>"2015-01-05T10:00:00.000+00:00", "title"=>"10:00 am to 10:30 am", "appointment_id"=>""},
    {"end"=>"2015-01-05T11:00:00.000+00:00", "start"=>"2015-01-05T10:30:00.000+00:00", "title"=>"10:30 am to 11:00 am", "appointment_id"=>""},
    {"end"=>"2015-01-05T11:30:00.000+00:00", "start"=>"2015-01-05T11:00:00.000+00:00", "title"=>"11:00 am to 11:30 am", "appointment_id"=>""},
    {"end"=>"2015-01-05T12:00:00.000+00:00", "start"=>"2015-01-05T11:30:00.000+00:00", "title"=>"11:30 am to 12:00 pm", "appointment_id"=>""},
    {"end"=>"2015-01-05T12:30:00.000+00:00", "start"=>"2015-01-05T12:00:00.000+00:00", "title"=>"12:00 pm to 12:30 pm", "appointment_id"=>""},
    {"end"=>"2015-01-05T13:00:00.000+00:00", "start"=>"2015-01-05T12:30:00.000+00:00", "title"=>"12:30 pm to  1:00 pm", "appointment_id"=>""},
    {"end"=>"2015-01-05T13:30:00.000+00:00", "start"=>"2015-01-05T13:00:00.000+00:00", "title"=>" 1:00 pm to  1:30 pm", "appointment_id"=>""},
    {"end"=>"2015-01-05T14:00:00.000+00:00", "start"=>"2015-01-05T13:30:00.000+00:00", "title"=>" 1:30 pm to  2:00 pm", "appointment_id"=>""},
    {"end"=>"2015-01-05T14:30:00.000+00:00", "start"=>"2015-01-05T14:00:00.000+00:00", "title"=>" 2:00 pm to  2:30 pm", "appointment_id"=>""},
    {"end"=>"2015-01-05T15:00:00.000+00:00", "start"=>"2015-01-05T14:30:00.000+00:00", "title"=>" 2:30 pm to  3:00 pm", "appointment_id"=>""},
    {"end"=>"2015-01-05T15:30:00.000+00:00", "start"=>"2015-01-05T15:00:00.000+00:00", "title"=>" 3:00 pm to  3:30 pm", "appointment_id"=>""},
    {"end"=>"2015-01-05T16:00:00.000+00:00", "start"=>"2015-01-05T15:30:00.000+00:00", "title"=>" 3:30 pm to  4:00 pm", "appointment_id"=>""},
    {"end"=>"2015-01-05T16:30:00.000+00:00", "start"=>"2015-01-05T16:00:00.000+00:00", "title"=>" 4:00 pm to  4:30 pm", "appointment_id"=>""},
    {"end"=>"2015-01-05T17:00:00.000+00:00", "start"=>"2015-01-05T16:30:00.000+00:00", "title"=>" 4:30 pm to  5:00 pm", "appointment_id"=>""},
    {"end"=>"2015-01-05T17:30:00.000+00:00", "start"=>"2015-01-05T17:00:00.000+00:00", "title"=>" 5:00 pm to  5:30 pm", "appointment_id"=>""},
    {"end"=>"2015-01-05T18:00:00.000+00:00", "start"=>"2015-01-05T17:30:00.000+00:00", "title"=>" 5:30 pm to  6:00 pm", "appointment_id"=>""},
    {"end"=>"2015-01-05T18:30:00.000+00:00", "start"=>"2015-01-05T18:00:00.000+00:00", "title"=>" 6:00 pm to  6:30 pm", "appointment_id"=>""},
    {"end"=>"2015-01-05T19:00:00.000+00:00", "start"=>"2015-01-05T18:30:00.000+00:00", "title"=>" 6:30 pm to  7:00 pm", "appointment_id"=>""},
    {"end"=>"2015-01-05T19:30:00.000+00:00", "start"=>"2015-01-05T19:00:00.000+00:00", "title"=>" 7:00 pm to  7:30 pm", "appointment_id"=>""}],
  start_and_end_time: Mon, 05 Jan 2015...Tue, 06 Jan 2015,
  deleted_at: nil,
  deleted_by_id: nil,
  created_at: Wed, 31 Dec 2014 16:21:59 IST +05:30,
  updated_at: Wed, 31 Dec 2014 16:21:59 IST +05:30>,
 #<ConsultingLocationDoctorSchedule:0x007fe7653f7380
  id: 2,
  consulting_location_doctor_id: 495,
  schedule_date: Tue, 06 Jan 2015 00:00:00 IST +05:30,
  slot_details:
   [{"end"=>"2015-01-06T09:30:00.000+00:00", "start"=>"2015-01-06T09:00:00.000+00:00", "title"=>" 9:00 am to  9:30 am", "appointment_id"=>""},
    {"end"=>"2015-01-06T10:00:00.000+00:00", "start"=>"2015-01-06T09:30:00.000+00:00", "title"=>" 9:30 am to 10:00 am", "appointment_id"=>""},
    {"end"=>"2015-01-06T10:30:00.000+00:00", "start"=>"2015-01-06T10:00:00.000+00:00", "title"=>"10:00 am to 10:30 am", "appointment_id"=>""},
    {"end"=>"2015-01-06T11:00:00.000+00:00", "start"=>"2015-01-06T10:30:00.000+00:00", "title"=>"10:30 am to 11:00 am", "appointment_id"=>""},
    {"end"=>"2015-01-06T11:30:00.000+00:00", "start"=>"2015-01-06T11:00:00.000+00:00", "title"=>"11:00 am to 11:30 am", "appointment_id"=>""},
    {"end"=>"2015-01-06T12:00:00.000+00:00", "start"=>"2015-01-06T11:30:00.000+00:00", "title"=>"11:30 am to 12:00 pm", "appointment_id"=>""},
    {"end"=>"2015-01-06T12:30:00.000+00:00", "start"=>"2015-01-06T12:00:00.000+00:00", "title"=>"12:00 pm to 12:30 pm", "appointment_id"=>""},
    {"end"=>"2015-01-06T13:00:00.000+00:00", "start"=>"2015-01-06T12:30:00.000+00:00", "title"=>"12:30 pm to  1:00 pm", "appointment_id"=>""},
    {"end"=>"2015-01-06T13:30:00.000+00:00", "start"=>"2015-01-06T13:00:00.000+00:00", "title"=>" 1:00 pm to  1:30 pm", "appointment_id"=>""},
    {"end"=>"2015-01-06T14:00:00.000+00:00", "start"=>"2015-01-06T13:30:00.000+00:00", "title"=>" 1:30 pm to  2:00 pm", "appointment_id"=>""},
    {"end"=>"2015-01-06T14:30:00.000+00:00", "start"=>"2015-01-06T14:00:00.000+00:00", "title"=>" 2:00 pm to  2:30 pm", "appointment_id"=>""},
    {"end"=>"2015-01-06T15:00:00.000+00:00", "start"=>"2015-01-06T14:30:00.000+00:00", "title"=>" 2:30 pm to  3:00 pm", "appointment_id"=>""},
    {"end"=>"2015-01-06T15:30:00.000+00:00", "start"=>"2015-01-06T15:00:00.000+00:00", "title"=>" 3:00 pm to  3:30 pm", "appointment_id"=>""},
    {"end"=>"2015-01-06T16:00:00.000+00:00", "start"=>"2015-01-06T15:30:00.000+00:00", "title"=>" 3:30 pm to  4:00 pm", "appointment_id"=>""},
    {"end"=>"2015-01-06T16:30:00.000+00:00", "start"=>"2015-01-06T16:00:00.000+00:00", "title"=>" 4:00 pm to  4:30 pm", "appointment_id"=>""},
    {"end"=>"2015-01-06T17:00:00.000+00:00", "start"=>"2015-01-06T16:30:00.000+00:00", "title"=>" 4:30 pm to  5:00 pm", "appointment_id"=>""},
    {"end"=>"2015-01-06T17:30:00.000+00:00", "start"=>"2015-01-06T17:00:00.000+00:00", "title"=>" 5:00 pm to  5:30 pm", "appointment_id"=>""},
    {"end"=>"2015-01-06T18:00:00.000+00:00", "start"=>"2015-01-06T17:30:00.000+00:00", "title"=>" 5:30 pm to  6:00 pm", "appointment_id"=>""},
    {"end"=>"2015-01-06T18:30:00.000+00:00", "start"=>"2015-01-06T18:00:00.000+00:00", "title"=>" 6:00 pm to  6:30 pm", "appointment_id"=>""},
    {"end"=>"2015-01-06T19:00:00.000+00:00", "start"=>"2015-01-06T18:30:00.000+00:00", "title"=>" 6:30 pm to  7:00 pm", "appointment_id"=>""},
    {"end"=>"2015-01-06T19:30:00.000+00:00", "start"=>"2015-01-06T19:00:00.000+00:00", "title"=>" 7:00 pm to  7:30 pm", "appointment_id"=>""},
    {"end"=>"2015-01-06T20:00:00.000+00:00", "start"=>"2015-01-06T19:30:00.000+00:00", "title"=>" 7:30 pm to  8:00 pm", "appointment_id"=>""},
    {"end"=>"2015-01-06T20:30:00.000+00:00", "start"=>"2015-01-06T20:00:00.000+00:00", "title"=>" 8:00 pm to  8:30 pm", "appointment_id"=>""}],
  start_and_end_time: Tue, 06 Jan 2015...Wed, 07 Jan 2015,
  deleted_at: nil,
  deleted_by_id: nil,
  created_at: Wed, 31 Dec 2014 16:21:59 IST +05:30,
  updated_at: Wed, 31 Dec 2014 16:21:59 IST +05:30>,
引发此错误:

ConsultingLocation.find(495).schedules.where("start_and_end_time :@> ?",Date.yesterday..Date.today)
  ConsultingLocation Load (0.5ms)  SELECT  "consulting_locations".* FROM "consulting_locations" WHERE "consulting_locations"."deleted_at" IS NULL AND "consulting_locations"."id" = $1 LIMIT 1  [["id", 495]]
PG::SyntaxError: ERROR:  syntax error at or near ":"
LINE 1: ...sulting_location_id" = $1 AND (start_and_end_time :@> '2014-...
但它没有起作用。通常如何使用日期范围


这也不起作用:

ConsultingLocation.find(495).schedules.where(start_and_end_time: Date.yesterday..Date.today)
  ConsultingLocation Load (103.0ms)  SELECT  "consulting_locations".* FROM "consulting_locations" WHERE "consulting_locations"."deleted_at" IS NULL AND "consulting_locations"."id" = $1 LIMIT 1  [["id", 495]]
PG::InvalidTextRepresentation: ERROR:  malformed range literal: "2015-01-01"
LINE 1: ...on_doctor_schedules"."start_and_end_time" BETWEEN '2015-01-0...
                                                             ^
DETAIL:  Missing left parenthesis or bracket.

如果要确定某个范围是否包含另一个范围:

where("start_and_end_time && daterange(?, ?, '[]')", Date.yesterday, Date.today)
如果您想知道某个日期是否在某个范围内:

where("start_and_end_time @> ?::date", Date.yesterday)
我发现这些文章很有帮助:


  • 你已经回答了你的问题,展示了它是如何工作的,为什么你要尝试另一种不起作用的方式?@MaurícioLinhares现在检查编辑。好吧,它不起作用,在ActiveRecord中自动查询日期的方法是使用
    表。where(开始和结束时间:Date.Date.昨天..Date.today)
    。不知道你为什么要尝试一些显然在这里不起作用的东西。@MaurícioLinhares这不起作用,我已经尝试过了!我并不是在尝试一些显然不起作用的东西,因为这些文档指向一些与我发布的内容类似的东西。我更新了这个问题,因为其他人可能也会这么认为。在rails文档中,您在哪里找到了您尝试使用的参考资料?
    where("start_and_end_time @> ?::date", Date.yesterday)