What is the proper way to define an array of values for the IN operator in Active Record
I need to search my data for rows who's value is contained in an array of strings. What is the proper way to do this in Active Record?
For example, say I have this array:
["fluffy", "spot"]
I want the SQL to look something like this:
select * FROM Pets WHERE name IN ('fluffy', 'spot')
The following works:
list = ["fluffy", "spot"].map { |x| "'#{x}'" }.join(', ')
Pet.where("name in (#{list})")
Obviously this is a bad idea. What is the correct method to do this? Is there a way to use paramet开发者_如何转开发ers in this way, or does Active Record have a special method for this?
list = ["fluffy", "spot"]
Pet.where(:name => list)
精彩评论