Greetings!
Is there some special syntax required to use the match operator (=~) in a find
operation? Or is it not possible? I''ve got some data items from an
external source that could be capitalized or not. So right now I''m
stuck with doing:
Item.find(:first,
:conditions => ["name = ? or name =?", Potatoes,
potatoes])
I''d prefer to do:
Item.find(:first,
:conditions => ["name =~ ?", /(P|p)otatoes])
The first works, but it doesn''t seem very Rails-like. I''m
getting nil results on the second.
TIA,
Bill
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://wrath.rubyonrails.org/pipermail/rails/attachments/20060530/dd387bf7/attachment.html
Hi,
You have a bit of a mix of syntax there - the conditions string is sql
so you can''t use the regular expressions in the way you stated. You
can
use the LIKE operator though:
Item.find(:first,
:conditions => ["name LIKE ?",
''potatoes''])
Hope that helps,
Steve
Bill Walton wrote:> Greetings!
>
> Is there some special syntax required to use the match operator (=~) in
> a find operation? Or is it not possible? I''ve got some data
items from
> an external source that could be capitalized or not. So right now
I''m
> stuck with doing:
>
> Item.find(:first,
> :conditions => ["name = ? or name =?", Potatoes,
potatoes])
>
> I''d prefer to do:
>
> Item.find(:first,
> :conditions => ["name =~ ?", /(P|p)otatoes])
>
> The first works, but it doesn''t seem very Rails-like.
I''m getting nil
> results on the second.
>
> TIA,
> Bill
>
>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
>
> ------------------------------------------------------------------------
>
> No virus found in this incoming message.
> Checked by AVG Free Edition.
> Version: 7.1.394 / Virus Database: 268.7.4/351 - Release Date: 29/05/2006
and if you don''t want to take case into account, you can set everything
downcase:
LOWER(name) LIKE :search_value, { search_value => variable.downcase}
for a string match, I usually add some % before/after:
{ search_value => ''%'' + variable.downcase +
''%'' }
because my match does not necessarily start or end by the pattern.
On 5/30/06, Stephen Bartholomew <sb@2404.co.uk>
wrote:>
> Hi,
>
> You have a bit of a mix of syntax there - the conditions string is sql
> so you can''t use the regular expressions in the way you stated.
You can
> use the LIKE operator though:
>
> Item.find(:first,
> :conditions => ["name LIKE ?",
''potatoes''])
>
> Hope that helps,
>
> Steve
>
>
> Bill Walton wrote:
> > Greetings!
> >
> > Is there some special syntax required to use the match operator (=~)
in
> > a find operation? Or is it not possible? I''ve got some data
items from
> > an external source that could be capitalized or not. So right now
I''m
> > stuck with doing:
> >
> > Item.find(:first,
> > :conditions => ["name = ? or name =?",
Potatoes, potatoes])
> >
> > I''d prefer to do:
> >
> > Item.find(:first,
> > :conditions => ["name =~ ?", /(P|p)otatoes])
> >
> > The first works, but it doesn''t seem very Rails-like.
I''m getting nil
> > results on the second.
> >
> > TIA,
> > Bill
> >
> >
> >
> >
> >
------------------------------------------------------------------------
> >
> > _______________________________________________
> > Rails mailing list
> > Rails@lists.rubyonrails.org
> > http://lists.rubyonrails.org/mailman/listinfo/rails
> >
> >
> >
------------------------------------------------------------------------
> >
> > No virus found in this incoming message.
> > Checked by AVG Free Edition.
> > Version: 7.1.394 / Virus Database: 268.7.4/351 - Release Date:
> 29/05/2006
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://wrath.rubyonrails.org/pipermail/rails/attachments/20060530/b65fe748/attachment.html
Nicholas, Stephen,
Thanks to you both. That puts me on the right track. Guess I''m still
recovering from the holiday ;-)
Best regards,
Bill
----- Original Message -----
From: Nicolas Buet
To: rails@lists.rubyonrails.org
Sent: Tuesday, May 30, 2006 8:02 AM
Subject: Re: [Rails] match operator in find
and if you don''t want to take case into account, you can set
everything downcase:
LOWER(name) LIKE :search_value, { search_value => variable.downcase}
for a string match, I usually add some % before/after:
{ search_value => ''%'' + variable.downcase +
''%'' }
because my match does not necessarily start or end by the pattern.
On 5/30/06, Stephen Bartholomew <sb@2404.co.uk> wrote:
Hi,
You have a bit of a mix of syntax there - the conditions string is sql
so you can''t use the regular expressions in the way you stated.
You can
use the LIKE operator though:
Item.find(:first,
:conditions => ["name LIKE ?",
''potatoes''])
Hope that helps,
Steve
Bill Walton wrote:
> Greetings!
>
> Is there some special syntax required to use the match operator (=~) in
> a find operation? Or is it not possible? I''ve got some data
items from
> an external source that could be capitalized or not. So right now
I''m
> stuck with doing:
>
> Item.find(:first,
> :conditions => ["name = ? or name =?",
Potatoes, potatoes])
>
> I''d prefer to do:
>
> Item.find(:first,
> :conditions => ["name =~ ?", /(P|p)otatoes])
>
> The first works, but it doesn''t seem very Rails-like.
I''m getting nil
> results on the second.
>
> TIA,
> Bill
>
>
>
>
>
------------------------------------------------------------------------
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
>
>
------------------------------------------------------------------------
>
> No virus found in this incoming message.
> Checked by AVG Free Edition.
> Version: 7.1.394 / Virus Database: 268.7.4/351 - Release Date:
29/05/2006
_______________________________________________
Rails mailing list
Rails@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails
------------------------------------------------------------------------------
_______________________________________________
Rails mailing list
Rails@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://wrath.rubyonrails.org/pipermail/rails/attachments/20060530/647c2e3f/attachment.html