Оптимизация запросов ActiveRecord. Можно ли объединить два запроса в один?
Мне нужно получить все записи и первую запись, которую я использую.
@appointments ||= @lead.appointments.order(created_at: :desc)
if appt = @appointments.first
json.latest_appointment do
json.partial! 'api/v1/lead_appointments/appointment', appointment: appt
end
end
json.appointments do
json.array! @appointments do |a|
json.partial! 'api/v1/lead_appointments/appointment', appointment: a
end
end
и получите аналогичный SQL-запрос
Appointment Load (0.4ms) SELECT "appointments".* FROM "appointments" WHERE "appointments"."lead_id" = $1 ORDER BY "appointments"."created_at" DESC LIMIT $2 [["lead_id", 760730], ["LIMIT", 1]]`
Appointment Load (0.4ms) SELECT "appointments".* FROM "appointments" WHERE "appointments"."lead_id" = $1 ORDER BY "appointments"."appointment_at" DESC [["lead_id", 760730]]`