Created
April 11, 2011 23:07
-
-
Save patmaddox/914573 to your computer and use it in GitHub Desktop.
Question about case-insensitive columns in Rails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? |
@patmaddox yeah, I got that after some tweets between you and @foca
I will say this: I understand you have a use case for it but this smells to me. Doing something like this feels about as dirty as setting a default_scope
on your model.
You people are all crazy. I just want you to know that :)
I'll accept that as praise
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@bcardarella maybe I'm missing something but is there a way to have that applied all the time? as I've mentioned, the point is to specify case-insensitivity once and only once.
@godfoca it's a good thing that when you work with me, we pair, so this won't be some shocking surprise. Also, my "solution (not yet implemented) is absolutely explicit. The rule is, "This is case-insensitive all the time." That's what my code says. By sprinkling calls to scopes everywhere, I've made the code verbose, but the rule is implicit.