views:

302

answers:

3

I am in the process of switching my development environment from sqlite3 to postgresql 8.4 and have one last hurdle.

In my original I had the following line in a helper method;

result = Users.find(:all, :order => "name collate NOCASE")

which provided a very nice case-insensitive search. I can't replicate this for postgresql. Should be easy - any ideas?

Thanks.

+2  A: 

IN SQL you could use ORDER BY LOWER(columnname), no idea how to do it in Ruby. A functional index (also on LOWER(columnname) ) will help to speed things up.

Frank Heikens
+5  A: 
result = Users.find(:all, :order => "LOWER(name)")

To take a little bit from both Brad and Frank.

LanceH
Nice, that worked thanks. Pointed me in the right direction to learn a bit more about this for myself. Using UPPER(name) also works and in fact is what has ended up in my code - for no particular reason.
brad
A more extreme solution I've resorted to is to have an extra column tracking the original column. Using triggers on insert or update, modify that column. I've used this in the past a couple times, where I wanted to sort titles of something and didn't want leading words of "a, an, the" to count. I also stripped out all special characters. This resulted in "title", "a title", "!title", "Title" sorting next to each other rather than scattered about. It also made it possible to edit the sortable field so I could make "title V" come before "title IX".
LanceH
+1  A: 

Have you considered storing your column as citext type? It really just internalizes the call to lower() as I understand it. It would be automatic for you afterwards. If there are times you need a case sensitive search, this may not be the best idea though.

rfusca
Interesting. That's a new type to me. A bit of reading suggests that you need to do install the citext module separately to enable this feature in 8.4 (but will be built in to 9.0). This would affect the portability of my code so I'll leave it out for now. I don't need to do a case sensitive search at this time but I think I would like to keep the option open at this time so will leave this for now. Very useful to know it exists though so thanks!
brad