# app/controllers/api/v1/schedulescontroller.rb class Api::V1::SchedulesController <…
- User
# app/controllers/api/v1/schedules_controller.rb
class Api::V1::SchedulesController < ApplicationController
def index
departure_station = params[:departure]
destination_station = params[:destination]
if departure_station.present? && destination_station.present?
schedules = Schedule.joins(stations: { schedule: { route: :bus_schedule } })
.where(stations: { name: departure_station })
.where(id: Schedule.joins(stations: { schedule: { route: :bus_schedule } })
.where(stations: { name: destination_station })
.pluck(:id))
.group(:trip)
.order(:trip)
render json: schedules.map { |schedule|
{
trip: schedule.trip,
stations: schedule.stations.map { |station|
{
station: station.name,
arrivalTime: station.arrival_time,
departureTime: station.departure_time
}
}
}
}, status: :ok
else
render json: { error: 'Departure and destination stations are required.' }, status: :unprocessable_entity
end
end
end
class Api::V1::SchedulesController < ApplicationController def index departure_station = params[:departure] destination_station = params[:destination]
if departure_station.present? && destination_station.present? schedules = Schedule.joins(stations: { schedule: { route: :bus_schedule } }) .where(stations: { name: departure_station }) .where(id: Schedule.joins(stations: { schedule: { route: :bus_schedule } }) .where(stations: { name: destination_station }) .pluck(:id)) .group(:trip) .order(:trip) render json: schedules.map { |schedule| { trip: schedule.trip, stations: schedule.stations.map { |station| { station: station.name, arrivalTime: station.arrival_time, departureTime: station.departure_time } }.reject { |s| s[:station] == departure_station || s[:station] == destination_station || station_sequence(s[:station], schedule.route_id) < station_sequence(departure_station, schedule.route_id) } } }, status: :ok else render json: { error: 'Departure and destination stations are required.' }, status: :unprocessable_entity endend
private
def station_sequence(station_name, route_id) Station.where(name: station_name, route_id: route_id).pluck(:sequence).first end end this returns below when i want manjali to athani [{"trip":1,"stations":[{"station":"ANGAMALY PRIVATE BUS STATION","arrivalTime":"06:35 am","departureTime":"06:35 am"},{"station":"ATHANI","arrivalTime":"06:53 am","departureTime":"06:53 am"},{"station":"MANJALY","arrivalTime":"07:15 am","departureTime":"07:15 am"},{"station":"NORTH PARAVUR","arrivalTime":"07:25 am","departureTime":"07:25 am"}]},{"trip":2,"stations":[{"station":"NORTH PARAVUR","arrivalTime":"07:30 am","departureTime":"07:30 am"},{"station":"MANJALY","arrivalTime":"07:40 am","departureTime":"07:40 am"},{"station":"ATHANI","arrivalTime":"08:02 am","departureTime":"08:02 am"},{"station":"ANGAMALY PRIVATE BUS STATION","arrivalTime":"08:20 am","departureTime":"08:20 am"}]},{"trip":3,"stations":[{"station":"ANGAMALY PRIVATE BUS STATION","arrivalTime":"08:38 am","departureTime":"08:38 am"},{"station":"ATHANI","arrivalTime":"08:56 am","departureTime":"08:56 am"},{"station":"MANJALY","arrivalTime":"09:18 am","departureTime":"09:18 am"},{"station":"NORTH PARAVUR","arrivalTime":"09:28 am","departureTime":"09:28 am"}]}]
but i only want
[{"trip":2,"stations":[{"station":"NORTH PARAVUR","arrivalTime":"07:30 am","departureTime":"07:30 am"},{"station":"MANJALY","arrivalTime":"07:40 am","departureTime":"07:40 am"},{"station":"ATHANI","arrivalTime":"08:02 am","departureTime":"08:02 am"},{"station":"ANGAMALY PRIVATE BUS STATION","arrivalTime":"08:20 am","departureTime":"08:20 am"}]}]
because other two are athani to manjali
- Assistant
It looks like your
station_sequencemethod works correctly to get the sequence number of a station within a given route_id. However, it appears the filtering of the stations within therender jsonblock using.reject { ... }is not functioning as needed. As a result, you're getting unwanted trips in your output that don't match the correct direction fromdeparture_stationtodestination_station.To ensure you only get the schedules that go from
departure_stationtodestination_station, the following adjustments can be made:- Fetch all the schedules that pass through both
departure_stationanddestination_station. This can be done by ensuring thedeparture_stationcomes before thedestination_stationon the route. - For each schedule, ensure that the itinerary from
departure_stationtodestination_stationis in the correct order.
Let's refactor the code to achieve the correct functionality:
class Api::V1::SchedulesController < ApplicationController def index departure_station = params[:departure] destination_station = params[:destination] if departure_station.present? && destination_station.present? schedules = Schedule.includes(:stations) # Include related stations to reduce DB queries .select { |schedule| schedule_has_correct_itinerary(schedule, departure_station, destination_station) } render json: schedules.map { |schedule| { trip: schedule.trip, stations: schedule.stations.select { |station| included_in_itinerary?(station, schedule, departure_station, destination_station) }.map { |station| { station: station.name, arrivalTime: station.arrival_time.strftime('%I:%M %p'), departureTime: station.departure_time.strftime('%I:%M %p') } } } }, status: :ok else render json: { error: 'Departure and destination stations are required.' }, status: :unprocessable_entity end end private def schedule_has_correct_itinerary(schedule, departure_station, destination_station) departure_index = station_index(schedule.stations, departure_station) destination_index = station_index(schedule.stations, destination_station) departure_index && destination_index && departure_index < destination_index end def included_in_itinerary?(station, schedule, departure_station, destination_station) departure_index = station_index(schedule.stations, departure_station) destination_index = station_index(schedule.stations, destination_station) current_station_index = station_index(schedule.stations, station.name) current_station_index && departure_index && destination_index && current_station_index > departure_index && current_station_index <= destination_index end def station_index(stations, station_name) stations.index { |station| station.name == station_name } end endHere's what was changed/improved:
- We're now using
includes(:stations)to tell Active Record to retrieve the associatedStationrecords in a single query. This minimizes the number of database calls (N+1 query problem). - Two new helper methods,
schedule_has_correct_itineraryandincluded_in_itinerary?, have been added to clean up and clarify the filtering logic in theindexmethod. schedule_has_correct_itinerarychecks if the given schedule contains both the departure and destination station, and if the departure station comes before the destination station.included_in_itinerary?determines if a station is part of the itinerary between the departure and destination stations of a given schedule.- This refactored version should now correctly filter out schedules that do not go from
departure_stationtodestination_station, and for those that do, only include the relevant stations in the itinerary.
Make sure to handle time zones correctly in
arrivalTimeanddepartureTimeformatting since%I:%M %pwill use the server's default time zone. If your stations have a different time zone, you should adjust the code to handle the time conversion accurately. - Fetch all the schedules that pass through both
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.