Match operator in find

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

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

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.

Nicholas, Stephen,

Thanks to you both. That puts me on the right track. Guess I’m still
recovering from the holiday :wink:

Best regards,
Bill
----- Original Message -----
From: Nicolas B.
To: [email protected]
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 B. [email protected] 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 W. 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
> [email protected]
> 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
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails



Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails