Greg Hauptmann
2009-Jan-16 00:47 UTC
ActiveRecord support for find by regular expressions?? Is this possible? / Is there a plugin for this?
Hi, Is there any way of extending ActiveRecord to do something like: <model_object>find_by_description_regex(/.*find this.*) ?? That is ActiveRecord support for find via use of regular expressions? Perhaps a plugin? Or is there a reason this doesn''t exist? -- Greg http://blog.gregnet.org/ --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Philip Hallstrom
2009-Jan-16 01:16 UTC
Re: ActiveRecord support for find by regular expressions?? Is this possible? / Is there a plugin for this?
> Is there any way of extending ActiveRecord to do something like: > <model_object>find_by_description_regex(/.*find this.*) ?? > > That is ActiveRecord support for find via use of regular expressions? > Perhaps a plugin? Or is there a reason this doesn''t exist?Model.find(:all, :conditions => ["column LIKE ?", "%find this%"]) Would do a simple substring search. In some databases the above will *NOT* find "FIND THIS" because LIKE is case sensitive. PostgreSQL is like that. You can use ILIKE in that case, but don''t know if that''s supported elsewhere. If you aren''t worried about database agnostics you can look to see if your particular database support regex conditions... For example http://www.postgresql.org/docs/8.3/static/functions-matching.html -philip --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Phlip
2009-Jan-16 01:37 UTC
Re: ActiveRecord support for find by regular expressions?? Is this possible? / Is there a plugin for this?
Greg Hauptmann wrote:> Hi, > > Is there any way of extending ActiveRecord to do something like: > <model_object>find_by_description_regex(/.*find this.*) ?? > > That is ActiveRecord support for find via use of regular expressions? > Perhaps a plugin? Or is there a reason this doesn''t exist?ActiveRecord''s job is to turn a common Ruby DSL into a big SQL statement. Anything that common back-ends can''t do, ActiveRecord can''t do. Google [mysql regular expressions] to see if someone has added that to your database back-end. Then use find_by_sql (and scrub any tainted data yourself, to prevent SQL injection attacks like this one: http://xkcd.com/327/ ) Until then, just use LIKE: part = ''A'' Foo.find_all_by_group_id(group_id, :conditions => [''name LIKE ?'', part + ''%'' ] ) The % is a wildcard for any length of string - like * in a fileglob match. -- Phlip --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Greg Hauptmann
2009-Jan-16 01:42 UTC
Re: ActiveRecord support for find by regular expressions?? Is this possible? / Is there a plugin for this?
thanks - I''m on mysql so I''m going to try a named_scope like below (haven''t run/tested it yet): named_scope :regex_matched, lambda { |regex_str| { :condition => [" description REGEXP ''?'' " , regex_str] } } On Fri, Jan 16, 2009 at 11:37 AM, Phlip <phlip2005-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Greg Hauptmann wrote: > > Hi, > > > > Is there any way of extending ActiveRecord to do something like: > > <model_object>find_by_description_regex(/.*find this.*) ?? > > > > That is ActiveRecord support for find via use of regular expressions? > > Perhaps a plugin? Or is there a reason this doesn''t exist? > > ActiveRecord''s job is to turn a common Ruby DSL into a big SQL statement. > > Anything that common back-ends can''t do, ActiveRecord can''t do. > > Google [mysql regular expressions] to see if someone has added that to your > database back-end. Then use find_by_sql (and scrub any tainted data > yourself, to > prevent SQL injection attacks like this one: http://xkcd.com/327/ ) > > Until then, just use LIKE: > > part = ''A'' > Foo.find_all_by_group_id(group_id, > :conditions => [''name LIKE ?'', part + ''%'' ] ) > > The % is a wildcard for any length of string - like * in a fileglob match. > > -- > Phlip > > > > >-- Greg http://blog.gregnet.org/ --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Matt Jones
2009-Jan-17 00:38 UTC
Re: ActiveRecord support for find by regular expressions?? Is this possible? / Is there a plugin for this?
One more thing - if your real code is searching for things in descriptions, you may be better off with a full-text indexer like Ferret, Sphinx or Solr. At least in MySQL, a regexp condition will have to scan every row to find matches, which can get slow in a hurry. --Matt Jones On Jan 15, 8:42 pm, "Greg Hauptmann" <greg.hauptmann.r...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> thanks - I''m on mysql so I''m going to try a named_scope like below (haven''t > run/tested it yet): > > named_scope :regex_matched, lambda { |regex_str| > { :condition => [" description REGEXP ''?'' " , regex_str] } > } > > > > On Fri, Jan 16, 2009 at 11:37 AM, Phlip <phlip2...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Greg Hauptmann wrote: > > > Hi, > > > > Is there any way of extending ActiveRecord to do something like: > > > <model_object>find_by_description_regex(/.*find this.*) ?? > > > > That is ActiveRecord support for find via use of regular expressions? > > > Perhaps a plugin? Or is there a reason this doesn''t exist? > > > ActiveRecord''s job is to turn a common Ruby DSL into a big SQL statement. > > > Anything that common back-ends can''t do, ActiveRecord can''t do. > > > Google [mysql regular expressions] to see if someone has added that to your > > database back-end. Then use find_by_sql (and scrub any tainted data > > yourself, to > > prevent SQL injection attacks like this one:http://xkcd.com/327/) > > > Until then, just use LIKE: > > > part = ''A'' > > Foo.find_all_by_group_id(group_id, > > :conditions => [''name LIKE ?'', part + ''%'' ] ) > > > The % is a wildcard for any length of string - like * in a fileglob match. > > > -- > > Phlip > > -- > Greghttp://blog.gregnet.org/--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---