ActiveRecord find

you'd have to do that at the db level as part of your condition, AFAIK.

in mysql, for example

find(:all, :conditions > ["UPPER(name) = ?", input.upcase], ...)

As far as I'm aware (certainly on MySQL and MSSQL), database queries are case-insensitive. As such, so will your AR queries:

find(:all, :conditions => ["name = ?", "STEVE"]

is the same as:

find(:all, :conditions => ["name = ?", "steve"]

I think what Richard is asking is if you can make the include?() method case insensitive. For example: "STEVE".include? "steve" -> false

what he needs is:

"STEVE".include? "steve" -> true

I'm not exactly sure of how to achieve this neatly - perhaps someone else has an idea?


For SQL Server, this depends how your schema is setup. By default it's case insensitive, but it can be made case sensitive if required.


> I think what Richard is asking is if you can make the include?() > method case insensitive. For example: > "STEVE".include? "steve" -> false

Only valid SQL is allowed. Use a LIKE statement in your where clause:

['name LIKE ?', 'steve']

You can use % as a wildcard on one or both sides of the query.

['name LIKE ?', '%steve%']

However at this point you're at the mercy of the database. All ActiveRecord does is builds the query and sends it to the connection adapter.

what do you mean by 'you're at the mercy of the database'? do you mean that this method would go thru connection adapter, while the first way, ActiveRecord will do something magic for you?

ActiveRecord uses that to generate an SQL query. That that does, however, is up to the database. You may find that mysql or mssql are both case insensitive, but postgresql will require ILIKE to work the same.

i think what he wants is match case-insensitive strings

ie, Ruby On Rails == ruby on rails

or any variation in case.

mysql comparisons are by default case-insensitive, so you don't have to worry. what you do have to worry about is, if your project is intended to support multiple databases and will be public, is to deal with case-sensitive comparisons where the database is case-sensitive by default or the user has made it case-sensitive.

I believe UPPER (or LOWER) is an ANSI sql function and should be supported by all databases. so in my opinion, i would go with

find(:all, :conditions => ["UPPER(col_name) = ?", input.upcase])

and that should do it.

keep in mind that the the :conditions param is either a sql string fragment

"col_name = '#{input}'"

an array

["col_name = ?", input] or ["col_name = :input", { :input => input }]

or a hash

{ :col_name => input }

all of these generate

"col_name = 'xyz'"

there is no shorthand method of accomplishing what you want.