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 stor开发者_如何学Cing right now, if necessary.
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.
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.
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.
精彩评论