liutis
liutis

Reputation: 425

How to make friendly id case insensitive?

How can i replicate twitter usernames in URL cas insensitive:

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.

Upvotes: 5

Views: 1033

Answers (1)

Simone Carletti
Simone Carletti

Reputation: 176472

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.

Upvotes: 7

Related Questions