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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On Sep 22, 8:17 am, Jables <brun3...-Meo6Lv8EUjg3uPMLIKxrzw@public.gmane.org> wrote:> 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> > What is the preferred way to do this? > > Thanks, > JB--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Mon, Sep 22, 2008 at 9:17 AM, Jables <brun3797-Meo6Lv8EUjg3uPMLIKxrzw@public.gmane.org> wrote:> > 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.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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks On Sep 22, 12:36 am, "Xavier Noria" <f...-xlncskNFVEJBDgjK7y7TUQ@public.gmane.org> wrote:> On Mon, Sep 22, 2008 at 9:17 AM, Jables <brun3...-Meo6Lv8EUjg3uPMLIKxrzw@public.gmane.org> wrote: > > > 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. > > 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---