Pieter Hugo
2010-Jul-14 16:30 UTC
How to do a case insensitive search with multiple elements?
Hi I have a smallish problem If a = ["Joe","John"] then Model.find_all_by_firstname(a) works well. Except when the case of the characters is in dispute - how can I make the finder method case insensitive? All the discussions I find concern themselves with the singular case, but not with the case of an array being passed. Thanks for reading this! Pieter -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Rob Nichols
2010-Jul-15 08:05 UTC
Re: How to do a case insensitive search with multiple elements?
Model.find_all_by_firstname(a.collect{|a| a.downcase}) -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2010-Jul-15 10:07 UTC
Re: Re: How to do a case insensitive search with multiple elements?
On 15 July 2010 09:05, Rob Nichols <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Model.find_all_by_firstname(a.collect{|a| a.downcase})Will that work if the names in the db include upper case chars? I think the compare in the find needs to be case insensitive rather than what is being compared against being forced to lower case. Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Rob Nichols
2010-Jul-15 11:18 UTC
Re: Re: How to do a case insensitive search with multiple elements?
Colin Law wrote:> On 15 July 2010 09:05, Rob Nichols <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> Model.find_all_by_firstname(a.collect{|a| a.downcase}) > > Will that work if the names in the db include upper case chars? I > think the compare in the find needs to be case insensitive rather than > what is being compared against being forced to lower case. > > ColinIf that''s the case, this will do it: class Model self.find_all_by_firstname(first_name) find(:all, :conditions => ["LOWER(firstname) = ?", first_name.downcase]) end end -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Rob Biedenharn
2010-Jul-15 11:43 UTC
Re: Re: Re: How to do a case insensitive search with multiple elements?
On Jul 15, 2010, at 7:18 AM, Rob Nichols wrote:> Colin Law wrote: >> On 15 July 2010 09:05, Rob Nichols <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >>> Model.find_all_by_firstname(a.collect{|a| a.downcase}) >> >> Will that work if the names in the db include upper case chars? I >> think the compare in the find needs to be case insensitive rather >> than >> what is being compared against being forced to lower case. >> >> Colin > > If that''s the case, this will do it: > > class Model > > self.find_all_by_firstname(first_name) > find(:all, :conditions => ["LOWER(firstname) = ?", > first_name.downcase]) > end > > end > -- >As long as you''re aware that using LOWER(first_name) will prevent any use of an index on first_name. You can also look at your particular database to see if it supports a case-insensitive match. MySQL is case-insensitive by default (so I''m guessing that''s not what you''re using ;-) If you''re using PostgreSQL, you probably could find this via Google: http://wiki.postgresql.org/wiki/FAQ#How_do_I_perform_regular_expression_searches_and_case-insensitive_regular_expression_searches.3F_How_do_I_use_an_index_for_case-insensitive_searches.3F Note the part that discusses the CITEXT type and the use of an index on the lower(column_name). -Rob Rob Biedenharn Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org http://AgileConsultingLLC.com/ rab-/VpnD74mH8+00s0LW7PaslaTQe2KTcn/@public.gmane.org http://GaslightSoftware.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.