Skip to content

Instantly share code, notes, and snippets.

@IlyaDonskikh
Last active August 29, 2015 14:11
Show Gist options
  • Save IlyaDonskikh/bf60bfe3c5287e427fea to your computer and use it in GitHub Desktop.
Save IlyaDonskikh/bf60bfe3c5287e427fea to your computer and use it in GitHub Desktop.
Sql
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
WHERE ctr.id = c.country_id AND a.city_id = c.id AND (ctr.name = ? OR ctr.code = ? OR c.name = ? OR c.code = ? OR a.code = ? OR a.name = ?)'
sql = ActiveRecord::Base.send(:sanitize_sql_array, [sql, name.upcase, name, name.upcase, name, name.upcase, name])
result_hash = ActiveRecord::Base.connection.execute(sql)
## Old sql query (same error)
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.id = c.country_id AND a.city_id = c.id
WHERE ctr.name = ? OR ctr.code = ? OR c.name = ? OR c.code = ? OR a.code = ? OR a.name = ?'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment