Forum: Ruby on Rails MySQL in dev, Postgres in prod - differences in "LIKE" query

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.
2b2c2a705ed12f8fb327c7b4c56456c6?d=identicon&s=25 Sean Hussey (seanhussey)
on 2006-03-29 21:25
(Received via mailing list)
Hi everyone,

I run MySQL in my dev environment, but Postgres in my production
environment (out of necessity).  I'm having trouble finding a way to
write a query with a LIKE condition that is supported as case
insensitive in both databases.

Right now, I have this:

@query = "m"  # for example
@people = Person.find(:all, :conditions => ["last_name LIKE ?", @query +
"%"])

This works in MySQL, but in Postgres, it's case sensitive.  If I
change it to support the Postgres syntax, it doesn't work in MySQL.
Is there a more Rails way to handle this?

Thanks!

Sean
6783a6831dbd3839a1f03b0f416c2c6f?d=identicon&s=25 Ian Harding (Guest)
on 2006-03-29 22:32
(Received via mailing list)
Postgres uses "ilike" for insensetive "like" comparisons.  Maybe mysql
uses it too.

Alternatively, maybe you can force the query string to lowercase in
ruby, and use something like "LOWER(last_name) LIKE ?"
Re:
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-03-29 23:09
(Received via mailing list)
On Wed, 2006-03-29 at 14:04 -0500, Sean Hussey wrote:
> @people = Person.find(:all, :conditions => ["last_name LIKE ?", @query + "%"])
>
> This works in MySQL, but in Postgres, it's case sensitive.  If I
> change it to support the Postgres syntax, it doesn't work in MySQL.
> Is there a more Rails way to handle this?
----
can you use @query.downcase instead of @query?

Don't you need "%" + @query.downcase + "%" #percent symbol on both
sides?

Craig
2b2c2a705ed12f8fb327c7b4c56456c6?d=identicon&s=25 Sean Hussey (seanhussey)
on 2006-03-29 23:54
(Received via mailing list)
That'll work!  I checked around more and I couldn't find a Rails way to
do it.

Thank you!

Sean
This topic is locked and can not be replied to.