Forum: Ruby on Rails match operator in find

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bill W. (Guest)
on 2006-05-30 16:50
(Received via mailing list)
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
Stephen B. (Guest)
on 2006-05-30 16:56
(Received via mailing list)
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
Nicolas B. (Guest)
on 2006-05-30 17:05
(Received via mailing list)
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.
Bill W. (Guest)
on 2006-05-30 17:49
(Received via mailing list)
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 B.
  To: removed_email_address@domain.invalid
  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. <removed_email_address@domain.invalid> 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
    > removed_email_address@domain.invalid
    > 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
    removed_email_address@domain.invalid
    http://lists.rubyonrails.org/mailman/listinfo/rails





------------------------------------------------------------------------------


  _______________________________________________
  Rails mailing list
  removed_email_address@domain.invalid
  http://lists.rubyonrails.org/mailman/listinfo/rails
This topic is locked and can not be replied to.