Skip to content

Instantly share code, notes, and snippets.

@IlyaDonskikh
Created December 22, 2014 10:40
Show Gist options
  • Save IlyaDonskikh/d01b047eecdf38ac95c1 to your computer and use it in GitHub Desktop.
Save IlyaDonskikh/d01b047eecdf38ac95c1 to your computer and use it in GitHub Desktop.
sql = 'SELECT ctr.id, ctr.code as country_code, ctr.name as country_name, c.id, c.country_id,
c.code as city_code, c.name as city_name, a.code as airport_code, a.name as airport_name, a.city_id
FROM countries AS ctr INNER JOIN cities AS c INNER JOIN airports AS a
ON (ctr.name = ? OR ctr.code = ? OR c.name = ? OR c.code = ? OR a.code = ? OR a.name = ?)
AND ctr.id = c.country_id AND a.city_id = c.id'
sql = ActiveRecord::Base.send(:sanitize_sql_array, [sql, name.upcase, name, name.upcase, name, name.upcase, name])
result_hash = ActiveRecord::Base.connection.execute(sql)
## Result (development)
SELECT ctr.id, ctr.code as country_code, ctr.name as country_name, c.id, c.country_id,
c.code as city_code, c.name as city_name, a.code as airport_code, a.name as airport_name, a.city_id
FROM countries AS ctr INNER JOIN cities AS c INNER JOIN airports AS a
ON (ctr.name = 'Москва' OR ctr.code = 'Москва' OR c.name = 'Москва' OR c.code = 'Москва' OR a.code = 'Москва'
OR a.name = 'Москва') AND ctr.id = c.country_id AND a.city_id = c.id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment