开发者

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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜