Boris
Boris

Reputation: 412

How do I save a lowercase version of username?

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

Answers (3)

kobaltz
kobaltz

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

spickermann
spickermann

Reputation: 106972

# in user model
before_validation :generate_lowercase_username

private
  def generate_lowercase_username
    self.lowercase_username = username.downcase
  end

Upvotes: 1

CDub
CDub

Reputation: 13354

@user.username = "AbCd"
@user.lowercase_username = @user.username.downcase
@user.save

Upvotes: 0

Related Questions