Created
September 8, 2017 01:48
-
-
Save bogardpd/ca8265bdcf0a4e7c322dac995ed755e2 to your computer and use it in GitHub Desktop.
Find all airports in a given set of regions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/airport.rb | |
... | |
# Take a collection of strings representing the starts of ICAO codes, and | |
# return an a hash of airports in the region, with airport ids as keys and | |
# IATA codes as values. | |
# Params: | |
# +icao_starts+:: An array of strings of the start of ICAO codes (i.e. EG, K) | |
def self.in_region_hash(icao_starts) | |
# Build a querystring of LIKE statements with question marks connected by ORs: | |
conditions = icao_starts.map{"icao_code LIKE ?"}.join(" OR ") | |
# Create an array of patterns to insert as parameters: | |
patterns = icao_starts.map{|start| "#{start}%"} | |
# Run the SQL query, expanding the patterns to insert at the question marks: | |
matching_airports = Airport.where(conditions, *patterns) | |
# Create a hash of airport IDs and IATA codes: | |
iata_hash = Hash.new | |
matching_airports.each{|airport| iata_hash[airport[:id]] = airport[:iata_code]} | |
return iata_hash | |
end | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment