# 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
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
- User
/workspaces/codespaces-rails/bus/app/controllers/api/v1/schedules_controller.rb:64: syntax error, unexpected symbol literal, expecting `do' or '{' or '(' ...on.arrival_time.strftime(‘%I:%M %p’), ... ^ /workspaces/codespaces-rails/bus/app/controllers/api/v1/schedules_controller.rb:64: unknown type of %string ...al_time.strftime(‘%I:%M %p’), ... ^~~~~ Extracted source (around line #64): 62 63 64 65 66 67
{ station: station.name, arrivalTime: station.arrival_time.strftime(‘%I:%M %p’), departureTime: station.departure_time.strftime(‘%I:%M %p’) } } - Assistant
The error you are encountering is due to the use of typographic (curly) quotes around the string format (
‘%I:%M %p’). In Ruby, you must use straight quotes for string literals ('%I:%M %p'). Here's the corrected part of your code:stations: schedule.stations.map { |station| { station: station.name, arrivalTime: station.arrival_time.strftime('%I:%M %p'), # Corrected quotes departureTime: station.departure_time.strftime('%I:%M %p') # Corrected quotes } }After making this fix, your code should not throw a syntax error when you run it. Make sure to update this in every place where you used the wrong quotes.
- User
undefined method `strftime' for "07:30 am":String Did you mean? strip Extracted source (around line #64): 62 63 64 65 66 67
{ station: station.name, arrivalTime: station.arrival_time.strftime('%I:%M %p'), departureTime: station.departure_time.strftime('%I:%M %p') } } - Assistant
The error message you're seeing indicates that
arrival_timeanddeparture_timeare already stored as strings in the format"07:30 am", rather than asTimeorDateTimeobjects. Since they're strings, they don't have thestrftimemethod, which is why you're getting anundefined methoderror.If your
arrival_timeanddeparture_timeare indeed already formatted correctly as strings, there is no need to callstrftimeon them. You can simply return these strings directly. Here's how you can update your code:stations: schedule.stations.map { |station| { station: station.name, arrivalTime: station.arrival_time, # Assuming arrival_time is a string like "07:30 am" departureTime: station.departure_time # Assuming departure_time is a string like "07:30 am" } }This change will handle the data as strings without attempting to format them. Your updated code should no longer throw the error stating that
strftimeis an undefined method. Just make sure this is consistent with how yourarrival_timeanddeparture_timeare stored and used across your application. - User
- Assistant
- User
it also return if route has any 1 of the station in it even though the trip array is empty its showing vehicle number
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.