Reputation: 412
How can I save a lowercase version of a username? I'm trying to do what was said in "How do I handle uppercase and lowercase characters in a custom url?".
I want to save both a lowercase and a case-sensitive version of usernames in two different columns.
Thank you.
Upvotes: 1
Views: 886
Reputation: 7070
You could add something like this in your model where lower_username
is the other username column in your table.
#Simply populates the field on the database
before_save :downcase_username
or
#If you have validation of the username (ie must be a unique field),
#you may want to assign the lowercase username before the validation occurs.
before_validation :downcase_username
Create the private method to populate the lowercase username column
private
def downcase_username
self.lower_username = self.username.downcase
end
Upvotes: 3
Reputation: 106972
# in user model
before_validation :generate_lowercase_username
private
def generate_lowercase_username
self.lowercase_username = username.downcase
end
Upvotes: 1
Reputation: 13354
@user.username = "AbCd"
@user.lowercase_username = @user.username.downcase
@user.save
Upvotes: 0