the stations are ordered by their sequence in the route. it should only return where destination station is after departure station. so modify the code without comparing the time
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 BusSchedule < ApplicationRecord
has_many :routes
end
app/models/route.rb
class Route < ApplicationRecord
belongs_to :bus_schedule
has_many :schedules
end
app/models/schedule.rb
class Schedule < ApplicationRecord
belongs_to :route
has_many :stations
end
app/models/station.rb
class Station < ApplicationRecord
belongs_to :schedule
end
To ensure the stations are ordered by their sequence in the route and only return where the destination station is after the departure station (based on their order in the schedule), we’ll need to modify the code to work without time comparison. Instead, we will make use of the sequence numbers (assuming they're indicated by identifiers or indices within each schedule).
Your current code structure is somewhat in line with this; however, it’s a bit confusing and mixes different ActiveRecord associations in ways that seem incorrect. I’ll provide a simplified way to obtain the required schedules and stations, assuming each Station has a sequence_number field that dictates its order in the route.
In this example, I'm assuming you are entering route sequence numbers into the database already, and I have named this field sequence_number. Depending on your actual schema, the field might be called differently (e.g., order, position), and you should adjust the code as necessary.
ruby
# 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)
.where(stations: { name: departure_station })
.where(Schedule.arel_table[:id].in(
Schedule.joins(:stations)
.where(stations: { name: destination_station })
.where('stations.sequence_number > stations_departures.sequence_number')
.where('stations.schedule_id = stations_departures.schedule_id')
.select(:id)
))
.select('schedules.*, stations.sequence_number AS departure_sequence_number')
.from('schedules, stations as stations_departures')
.where('schedules.id = stations_departures.schedule_id')
.where('stations_departures.name': departure_station)
.distinct
.order('departure_sequence_number')
render json: schedules.map { |schedule|
{
trip: schedule.id, # Assuming trip corresponds to schedule.id
stations: schedule.stations.order(:sequence_number).map do |station|
{
station: station.name,
arrivalTime: station.arrival_time.strftime('%I:%M %p'),
departureTime: station.departure_time.strftime('%I:%M %p')
}
end.select { |s| s[:sequence_number] > schedule.departure_sequence_number }
}
}, status: :ok
else
render json: { error: 'Departure and destination stations are required.' }, status: :unprocessable_entity
end
end
end
# app/models/schedule.rb
class Schedule < ApplicationRecord
belongs_to :route
has_many :stations, -> { order(:sequence_number) } # Ensures stations are ordered by their sequence
end
# app/models/station.rb
class Station < ApplicationRecord
belongs_to :schedule
end
In this code, sequence_number should be the column in your database that indicates the station sequence within its schedule. The stations.sequence_number is used to compare the relative positions of stations in the route.
I’ve used schedule.id for trip because your previous code does not describe where trip comes from. The distinct call ensures that duplicates are removed if a schedule would otherwise be included multiple times.
Note: This code assumes that the stations have a sequence_number field used to determine their order in a schedule. If you have a different set up in your models, you should adjust the code to rely on the actual field that you are using to determine the order of stations within a route.