Thanks David.
That's almost exactly what I did.  However, I used username_down
instead of _key.  Don't know why you needed to know that, but...
you're welcome.

Anyway, after looking into this even more, I found that some databases
are case insensitive by default.  So instead of this being a rails
"problem," I think it's more of a database issue since some databases
will find the correct result no matter the casing.  I think it all
depends on which database you use (postgresql, mysql, etc) and how
they're encoded.

Either way, if the database is case Insensitive, well I at least now
know the fix.  Thanks again David.


On Feb 23, 3:44 pm, David Cuddeback <[email protected]> wrote:
> GoodGets,
>
> The way I solved this in my app was to have two columns for the
> username.  One is called "username," which stores the username the way
> the user wants it to be displayed (with capitalization), and one
> called "username_key," which is the username converted to lowercase.
> In my User model, I have this method:
>
>   def username=(value)
>     write_attribute :username, value
>     write_attribute :username_key, (value ? value.downcase : nil)
>   end
>
> With a unique index on "username_key," I'm sure that the database will
> enforce unique usernames without case sensitivity and I can preserve
> the capitalization that the user prefers.  I use
> User.find_by_username_key(username.downcase) to find users.
>
> -David
>
>
>
> On Mon, Feb 22, 2010 at 7:30 PM, GoodGets <[email protected]> wrote:
> >> Yep, but you can't have it both ways.  If you have users JoeBob and
> >> JOEbob and the browser requests "JoEbOb", which record do you return?
> >> You have to pick one and half the time you'll pick wrong.
>
> >> What you could do is require uniqueness on username's regardless of
> >> caps, but let them use caps to make username's easier to read... then
> >> you wouldn't have the conflict.
>
> > That's what I was doing
> > validates_uniqueness_of :username, :case_sensitive => false
> > which prevents JoeBob and JOEbob from signing up.  And that might be
> > the way it has to stay,
>
> > But, this got me thinking:
> > User.find_by_username(params[:user].downcase)
> > This will find the correct person in the db and return no errors, no
> > matter how many capitalizations someone uses in the URL.  But this
> > only works if the original user signed up with no capitalizations in
> > his/her name.
>
> > So, if the user signed up as joebob, then JOEbob, JoEbOb, or any other
> > variation will return the correct result, AND it will leave the URL
> > just as the user entered it, caps and all.
>
> > But, this doesn't work if the user originally signed up with caps in
> > his name.  So, what if I were to create an index in the table for the
> > usernames and pass in the option downcase.  I think it would work
> > perfectly then, but I'm not sure if this can be done?
>
> > --
> > You received this message because you are subscribed to the Google Groups 
> > "Ruby on Rails: Talk" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to 
> > [email protected].
> > For more options, visit this group 
> > athttp://groups.google.com/group/rubyonrails-talk?hl=en.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to