1

小文字バージョンのユーザー名を保存するにはどうすればよいですか? 「カスタムURLで大文字と小文字を処理するにはどうすればよいですか? 」で言われたことをやろうとしています。

ユーザー名の小文字バージョンと大文字と小文字を区別するバージョンの両方を 2 つの異なる列に保存したいと考えています。

ありがとうございました。

4

3 に答える 3

3

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
于 2013-10-11T17:42:22.000 に答える
1
# in user model
before_validation :generate_lowercase_username

private
  def generate_lowercase_username
    self.lowercase_username = username.downcase
  end
于 2013-10-11T17:49:53.030 に答える
0
@user.username = "AbCd"
@user.lowercase_username = @user.username.downcase
@user.save
于 2013-10-11T17:42:13.277 に答える