Skip to content

Instantly share code, notes, and snippets.

Created Sep 8, 2017
What would you like to do?
Find all airports in a given set of regions
# 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_code LIKE ?"}.join(" OR ")
# Create an array of patterns to insert as parameters:
patterns ={|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 =
matching_airports.each{|airport| iata_hash[airport[:id]] = airport[:iata_code]}
return iata_hash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment