activerecord - Rails: Dynamic Select with Active Record -


i need create dynamic sql select statement city, state combination table called metros least distance set of passed in gps coordinates. note set of passed in gps coordinates variables pull list. in order calculate distance need able pass in latitude , longitude, , using the haversine formula, calculate distance between present row , 291 cities in table metros, select minimum city,state.

in rails issue running how create select statement metros table allow passing in variable gps coordinates using active record instead of conventional sql.

at present, how far have gotten:

    metros.select(         "major_city         , major_state         ," haversine(row[latitude], row[longitude], lat2, long2)" 'distance'")            .group("major_city,major_state").limit(1).order('distance') 

row[latitude], row[longitude] passed in variable latitude , longitude (the present row comparing 291 cities/states gps coordinates in metros table , need select 1 least distance outputted haversine function). lat2, long2, need refer latitude , longitude columns in metros table.

in building query unaware of how input haversine function pull records database active record , calculate.

is there better way trying do?

you should implement haversine sql function , call in activerecord so:

metros.select("major_city, major_state, haversine(?, ?, lat, long) 'distance'", lat, long).   group("major_city,major_state").   limit(1).   order('distance') 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -