Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make friendly id case insensitive?

How can i replicate twitter usernames in URL cas insensitive:

  • www.site.com/users/My_Name
  • www.site.com/users/my_NAMe
  • www.site.com/users/MY_NAME

And that all these URL's, opens user page when he has username saved as "MY_Name" or etc.

And Model.find('my_NAmE') method finds user record named MY_Name.

like image 422
liutis Avatar asked Oct 01 '12 12:10

liutis


1 Answers

The way you make comparison case insensitive is to normalize the case both in the database and in the search input, so that the search always returns the result no matter the input case.

  1. Always downcase usernames before saving it to the database

  2. Use Model.find(params[:id].to_s.downcase) or custom method to perform a case insensitive find

There you are.

If you don't want to alter the original username case, then add a second column to the user table where you store the downcased version. Then perform the search on this field instead of the original user field.

like image 139
Simone Carletti Avatar answered Sep 29 '22 23:09

Simone Carletti