I learned that using field in (a1,a2,a3)
is comparably faster than using = operator.
SELECT *
FROM user
WHERE Greeting IN ('hello', 'hi', 'hey')
How can I write above query in rails way?
You'd do something like this:
users = User.where('greeting in (?)', %w{hello hi hey})
Rails will know what to do with an array, %w{hello hi hey}
, as a value for a placeholder. Or, if you already had an array of strings:
h_words = [ 'hello', 'hi', 'hey' ]
users = User.where('greeting in (?)', h_words)
# or even this
users = User.where('greeting in (:words)', :words => h_words)
or, probably the most Railsy way, like this:
users = User.where(:greeting => %w{hello hi hey})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.