Hi, I have an array that contains list IDs. I want to be able to use all of those list IDs to create an OR query. My code is: Contact_List.find(:all, :select => ''DISTINCT contact_id'', :conditions => [''list_id = ?'', params[:joblist][:list_id]]) but this yields the following query which is obviously incorrect: SELECT DISTINCT contact_id FROM contact_lists WHERE (list_id = ''2'',''3'') How can I get a query that reads: SELECT DISTINCT contact_id FROM contact_lists WHERE (list_id = ''2'' OR list_id = ''3'') Thanks, David -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
David Lelong wrote:> Hi, > > I have an array that contains list IDs. I want to be able to use all of > those list IDs to create an OR query. > > My code is: > > Contact_List.find(:all, > :select => ''DISTINCT contact_id'', > :conditions => [''list_id = ?'', params[:joblist][:list_id]]) > > but this yields the following query which is obviously incorrect: > > SELECT DISTINCT contact_id FROM contact_lists WHERE (list_id = ''2'',''3'') > > How can I get a query that reads: > > SELECT DISTINCT contact_id FROM contact_lists WHERE (list_id = ''2'' OR > list_id = ''3'') > > Thanks, > > DavidUsing an "in" clause should accomplish the same thing: Contact_List.find(:all, :select => ''DISTINCT contact_id'', :conditions => [''list_id in ?'', "(''" + params[:joblist][:list_id].join("'',''") + "'')"]) No chance to test for you, but I think the above should result in: SELECT DISTINCT contact_id FROM contact_lists WHERE (list_id in (''1'',''2'',''3'',''4'')) And give you the results you''re after. c. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Cayce Balara wrote:> Using an "in" clause should accomplish the same thing: > Contact_List.find(:all, > :select => ''DISTINCT contact_id'', > :conditions => [''list_id in ?'', "(''" + > params[:joblist][:list_id].join("'',''") + "'')"])Also no testing, but I believe Rails will do the joining for you (which is partly why his original attempt resulted in the way it did). So the above code can be simplified to: Contact_List.find :all, :select => ''DISTINCT contact_id'', :conditions => [''list_id IN (?)'', params[:joblist][:list_id]] Eric --~--~---------~--~----~------------~-------~--~----~ 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 11/21/06, David Lelong <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > > > Contact_List.find(:all, > :select => ''DISTINCT contact_id'', > :conditions => [''list_id = ?'', > params[:joblist][:list_id]]) > >Try with this: Contact_List.find_by_list_id(params[:joblist][:list_id]], :select => ''DISTINCT contact_id'') --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---