Skip to content

Instantly share code, notes, and snippets.

@nysalor
Created February 26, 2016 02:53
Show Gist options
  • Save nysalor/97b9499e89f2ea9e3b6a to your computer and use it in GitHub Desktop.
Save nysalor/97b9499e89f2ea9e3b6a to your computer and use it in GitHub Desktop.
ActiveRecord "IN" statement for multi columns condition
query_params = [['pochi', 'dog', 'black'], ['tama', 'cat', 'mike'], ['baron', 'horse', 'kurige']]
placeholder = query_params.map { (?, ?, ?) }
args = ["SELECT id FROM animals WHERE (name, race, color) IN (#{placeholder})", *query_params]
ids = ActiveRecord::Base.connection.select_all(ActiveRecord::Base.send(:sanitize_sql_array, args)).map {|x| x['id'] }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment