如何编写带有日期和数组查找的 Ruby where 子句?

How do I write a Ruby where clause with dates and array lookup?

local_appointment_records = ExternalAppointment.where(external_id: appointment_ids, deleted_by: nil, ('start_time BETWEEN ? AND ?',@date_from, @date_to) )

我想做类似上面的事情。 我首先要检查模型外部ID是否在appointment_ids的数组中,然后确保它没有被软删除,然后检查记录开始时间是否在日期之间。 非常感谢任何帮助。

您不需要使用 SQL 字符串来创建该查询。只需使用一个范围:

local_appointment_records = ExternalAppointment.where(
  external_id: appointment_ids, 
  deleted_by: nil, 
  start_time: @date_from..@date_to
)

如果您想使用 SQL 字符串,您希望链接调用:

local_appointment_records = ExternalAppointment.where(
  external_id: appointment_ids, 
  deleted_by: nil
).where(
  'start_time BETWEEN ? AND ?', @date_from, @date_to
)