Created
February 26, 2016 02:53
-
-
Save nysalor/97b9499e89f2ea9e3b6a to your computer and use it in GitHub Desktop.
ActiveRecord "IN" statement for multi columns condition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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