it only retrive departure and destination stations, also retrive stations in between…
- User
it only retrive departure and destination stations, also retrive stations in between them
class Api::V1::SchedulesController < ApplicationController def index departure_station = params[:departure] destination_station = params[:destination]
if departure_station.present? && destination_station.present? # Start by fetching schedules that include both departure and destination stations schedules = Schedule.joins(:stations, route: :bus_schedule) .where(stations: { name: [departure_station, destination_station] }) .distinct .includes(:stations, route: :bus_schedule) # Iterate through schedules to filter valid ones and prepare the response data vehicle_trips = schedules.each_with_object({}) do |schedule, acc| # Extract station names to check the order without triggering extra queries station_names = schedule.stations.map(&:name) puts station_names # Ensure that the destination comes after the departure if station_names.include?(departure_station) && station_names.include?(destination_station) && station_names.index(departure_station) < station_names.index(destination_station) vehicle_number = schedule.route.bus_schedule.vehicle_number trip_data = { trip: schedule.trip, stations: schedule.stations.map do |station| { station: station.name, arrivalTime: station.arrival_time, departureTime: station.departure_time } end } # Accumulate trips for each vehicle_number acc[vehicle_number] ||= [] acc[vehicle_number] << trip_data end end # Render the final JSON response render json: vehicle_trips.map { |vehicle_number, trips| { vehicle_number: vehicle_number, trips: trips } }, status: :ok else render json: { error: 'Departure and destination stations are required.' }, status: :unprocessable_entity endend end
- Assistant
- User
/workspaces/codespaces-rails/bus/app/controllers/api/v1/schedules_controller.rb:132: syntax error, unexpected local variable or method, expecting ')' ...e_station] + 1)…stations_hash[destination_station]) ... ^~~~~~~~~~~~~~~~
- Assistant
My apologies, it seems there was a typo in the provided range within the
whereclause. Also, I see that using the spread (…) instead of the triple-dot (...) punctuation for the range was mistaken. Let's correct this:intermediate_stations = schedule.stations.where(position: (stations_hash[departure_station] + 1)...stations_hash[destination_station]) .order(position: :asc)Here's the corrected part of the method:
# Find intermediate stations based on their position intermediate_stations = schedule.stations.where(position: (stations_hash[departure_station] + 1)...stations_hash[destination_station]) .order(position: :asc)Make sure you use the triple-dot (
...) in Ruby to create a range that excludes the last value, which is yourdestination_stationposition.The triple-dot
...is used to create a range that excludes the high end of the range. For example,1...3will include1and , but not . On the other hand, the double-dot creates a range that includes the high end. In your scenario, you don't want to include the into the intermediate stations, as you'll add it separately later.