小文字バージョンのユーザー名を保存するにはどうすればよいですか? 「カスタムURLで大文字と小文字を処理するにはどうすればよいですか? 」で言われたことをやろうとしています。
ユーザー名の小文字バージョンと大文字と小文字を区別するバージョンの両方を 2 つの異なる列に保存したいと考えています。
ありがとうございました。
小文字バージョンのユーザー名を保存するにはどうすればよいですか? 「カスタムURLで大文字と小文字を処理するにはどうすればよいですか? 」で言われたことをやろうとしています。
ユーザー名の小文字バージョンと大文字と小文字を区別するバージョンの両方を 2 つの異なる列に保存したいと考えています。
ありがとうございました。
lower_username
テーブルの他のユーザー名列であるモデルに、このようなものを追加できます。
#Simply populates the field on the database
before_save :downcase_username
また
#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
小文字のユーザー名列を設定するプライベート メソッドを作成する
private
def downcase_username
self.lower_username = self.username.downcase
end
# in user model
before_validation :generate_lowercase_username
private
def generate_lowercase_username
self.lowercase_username = username.downcase
end
@user.username = "AbCd"
@user.lowercase_username = @user.username.downcase
@user.save