Skip to content

Instantly share code, notes, and snippets.

@patmaddox
Created April 11, 2011 23:07
Show Gist options
  • Save patmaddox/914573 to your computer and use it in GitHub Desktop.
Save patmaddox/914573 to your computer and use it in GitHub Desktop.
Question about case-insensitive columns in Rails
Hola Rails people. I have a column in my database that I need to treat
as case-insensitive. The standard solution to this is to downcase the
value before inserting it into the database, and when doing
searches. My problems with this are:
1. I have to make calls to String#downcase in several places where I
*really* mean "this is case-insensitive"
2. I can only provide automatic-downcasing when going through the API
I provide. Anyone calling MyClass.where(:name => foo) has to know to
pass in a downcased version of foo.
I can think a few possible solution areas off the top of my head:
1. Do everything in the database. How exactly? Maybe I can create a
virtual column or view, and have a trigger on the db. DBMS is
Postgres, whatever version Heroku runs :)
2. A gem or plugin that treats entire columns as case-insentive by
automatically downcasing stuff as well as hooking into any arbitrary
SQL queries and dowcasing them.
3. Something much simpler that I haven't thought of :)
What do you guys know?
@bcardarella
Copy link

I'll accept that as praise

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment