Skip to content
Advertisement

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) )

I want to do something like the above. I first want to check if the model external id is in the array of appointment_ids, then make sure it is not soft deleted and then check if the record start time is between dates.. Any help much appreciated.

Advertisement

Answer

You don’t need to use a SQL string to create that query. Just use a range:

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

If you want to use a SQL string you want to chain the calls:

local_appointment_records = ExternalAppointment.where(
  external_id: appointment_ids, 
  deleted_by: nil
).where(
  'start_time BETWEEN ? AND ?', @date_from, @date_to
)
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement