Searching for multiple possible values

Hi,   I'm trying to do an search for multiple possible values in one database field. I have an array and want to find any of the values in the array in the database and return the results.

So basically arr = ['item1', 'item2', 'item3']

and I want to Model.find :all, :conditions => {:items => arr} and find all the entries where :items equals any of the strings in array.

What is the preferred way to do this?

Thanks, JB

Hi, I'm trying to do an search for multiple possible values in one database field. I have an array and want to find any of the values in the array in the database and return the results.

So basically arr = ['item1', 'item2', 'item3']

and I want to Model.find :all, :conditions => {:items => arr} and find all the entries where :items equals any of the strings in array.

Assuming :items is the name of a database field, that will do exactly what you want (ie it will generate "items in ('item1', 'item2', 'item3'))

Fred

IN is supposed to be faster than OR. In addition {:items => arr} is very concise, that's the recommended solution.

Take into account that if the array is huge the SQL may be too large. In MySQL for example IN() has no limit in the number of arguments it may receive, but a single SQL over the wire cannot exceed max_allowed_packet.

Thanks