kateray
kateray

Reputation: 2246

How do I remove case sensitivity from an entire Rails app?

I have a fairly complex Rails app built on top of Twitter's API, and want to make the whole app case-insensitive.

Up until now, we've been converting all usernames and other strings to .downcase whenever possible and using the default case-sensitive searches, but that's starting to cause problems, with 'Username' and 'username' being considered different users.

Do I need to set this validation:

validates_uniqueness_of :username, :case_sensitive => false

somewhere on every one of my models (there are a lot) and remove all instances of .downcase from the app (there are a TON)? Any other ideas?

Note: this app isn't live yet, so I don't mind wiping all the data it's storing right now, if necessary.

Upvotes: 2

Views: 689

Answers (3)

kateray
kateray

Reputation: 2246

I solved it using Rack Middleware, see this page: http://gehling.dk/2010/02/how-to-make-rails-routing-case-insensitive/

UPDATE: This code has been updated to work with Rails 3 as well, so still a very good option.

Upvotes: 1

Winfield
Winfield

Reputation: 19145

You could build a mixin that had your common validations like that username validation, add the case insensitivity, and just mix that in to all your models.

Upvotes: 0

jasonpgignac
jasonpgignac

Reputation: 2306

Perhaps setting a filter on the create option that converts all the data to downcase BEFORE validation? Then, all the data would be assumed to be in downcase. Otherwise, just write a custom validator.

Upvotes: 0

Related Questions