I am developing app which needs to query a timetable. The user specifies where they want to go from (start_station) and to (end_station) and the time (start_time) they wish to leave. My models are as follows: Provdier -> Service -> Run -> Stop <- Station -> = has many Provider = Bus Company Service = eg. Number 10 bus Run = Column on timetable Stop = Cell on timetable Station = Row on timetable Really provider and service dont come in to the equation and can be ignored. Station_A 09:45 11:23 12:01 2:30 Station_B 09:47 11:25 12:05 2:33 Station_C 09:55 11:31 12:15 2:37 Station_D 10:10 11:42 12:23 2:43 Station_E 10:25 11:45 12:30 2:51 So I need to find all runs (columns) where the end_station follows the start_station. This is the code I have at the moment, which finds two stations in the same run, and does not take in to account either the start_time or end_station coming after start_station. It works but is *very* database intensive, and that with only a small amount of sample data. This stikes me as a problem which has a cute solution. # params[:search][:start_station].class => string # params[:search][:end_station].class => string # params[:search][:start_time].class => time # Do actual search @matches = Array.new @start_station = Station.find(:first, :conditions => 'name = "' + params[:search][:start_station] + '"', :include => :stops) @end_station = Station.find(:first, :conditions => 'name = "' + params[:search][:end_station] + '"') @start_station.stops.each do |stop| @joining_stops = stop.run.stops @joining_stops.each do |stop| if stop.station.id = @end_station.id @matches.push(stop.run) break end end end Can anyone suggest a better(simpler) way of approching this problem?
on 2006-03-29 14:55
on 2006-05-16 14:47
Maybe use .collection and .uniq to get the results without duplicates?