I am trying to use a like clause with my rails app but am having some problems. I have tried customers = Customer.find(:all, :conditions => ["name LIKE ?", %some_variable%]) i have also tried customers = Customer.find_by_sql("where name LIKE ''%some_variable%'') and customers = Customer.find_by_sql("SELECT * FROM customers WHERE name LIKE ''some_variable''") The top 2 queries return results but not the results i would expect if some_variable is james i get about 100 rows returned with all sorts of names not just james Can anyone help suggest what i''m doing wrong or point me in the right direction? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
You missed Customer.find(:all, :conditions => ["name LIKE ?", "%#{some_variable}%"]) On Oct 8, 5:05 pm, dodgyboz <dodgy...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I am trying to use a like clause with my rails app but am having some > problems. I have tried > > customers = Customer.find(:all, :conditions => ["name LIKE ?", > %some_variable%]) > > i have also tried > > customers = Customer.find_by_sql("where name LIKE ''%some_variable%'') > > and > > customers = Customer.find_by_sql("SELECT * FROM customers WHERE name > LIKE ''some_variable''") > > The top 2 queries return results but not the results i would expect if > some_variable is james i get about 100 rows returned with all sorts of > names not just james > > Can anyone help suggest what i''m doing wrong or point me in the right > direction?--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I think you''ll find that you only need to use #{variable} when the variable is inside quotations. This problem is not one of using the correct rails syntax in this instance but thanks for the reply On 8 Oct, 17:10, andrewbruce <bruciemo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> You missed Customer.find(:all, :conditions => ["name LIKE ?", > "%#{some_variable}%"]) > > On Oct 8, 5:05 pm, dodgyboz <dodgy...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I am trying to use a like clause with my rails app but am having some > > problems. I have tried > > > customers = Customer.find(:all, :conditions => ["name LIKE ?", > > %some_variable%]) > > > i have also tried > > > customers = Customer.find_by_sql("where name LIKE ''%some_variable%'') > > > and > > > customers = Customer.find_by_sql("SELECT * FROM customers WHERE name > > LIKE ''some_variable''") > > > The top 2 queries return results but not the results i would expect if > > some_variable is james i get about 100 rows returned with all sorts of > > names not just james > > > Can anyone help suggest what i''m doing wrong or point me in the right > > direction?--~--~---------~--~----~------------~-------~--~----~ 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 10/8/07, dodgyboz <dodgyboz-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I think you''ll find that you only need to use #{variable} when the > variable is inside quotations. This problem is not one of using the > correct rails syntax in this instance but thanks for the replyNo, andrewbruce gave you the proper solution. Look closely at what he showed you. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
When using the LIKE modifier in Mysql, the % matches one or more characters and the _ match a single character. Hence the "%#{some_variable}%" would match anything containing the value of some_variable. -Bill dodgyboz wrote:> I think you''ll find that you only need to use #{variable} when the > variable is inside quotations. This problem is not one of using the > correct rails syntax in this instance but thanks for the reply > > On 8 Oct, 17:10, andrewbruce <bruciemo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> You missed Customer.find(:all, :conditions => ["name LIKE ?", >> "%#{some_variable}%"]) >> >> On Oct 8, 5:05 pm, dodgyboz <dodgy...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> >>> I am trying to use a like clause with my rails app but am having some >>> problems. I have tried >>> >>> customers = Customer.find(:all, :conditions => ["name LIKE ?", >>> %some_variable%]) >>> >>> i have also tried >>> >>> customers = Customer.find_by_sql("where name LIKE ''%some_variable%'') >>> >>> and >>> >>> customers = Customer.find_by_sql("SELECT * FROM customers WHERE name >>> LIKE ''some_variable''") >>> >>> The top 2 queries return results but not the results i would expect if >>> some_variable is james i get about 100 rows returned with all sorts of >>> names not just james >>> >>> Can anyone help suggest what i''m doing wrong or point me in the right >>> direction? >>> > > > > >-- Sincerely, William Pratt --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---