views:

26

answers:

1

I have a model with several attributes, among them title and artist.

The case of title and artist should be ignored in all the Active Record finders.

Basically, if title or artist are present in the :conditions (or dynamically i.e. find_all_by_artist), then the WHERE artist = :artist should become WHERE UPPER(artist) = UPPER(:artist) or something along these lines.

Is there a way of doing it with Rails?

+1  A: 

If you are using MySQL database, searches are case insensitive by default. Read the MySQL documentation for more details.

So finder call such as:

Album.find_all_by_artist("Nine Inch Nails").each do |album|
  p album.title
end

Is same as the call below:

Album.find_all_by_artist("NINE INCH NAILS").each do |album|
  p album.title
end

Both queries will print the same result:

# -> Pretty Hate Machine
# -> Broken
# -> ....
# -> The Slip
KandadaBoggu