Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
空白を除去するString型
class StrippedString < ActiveModel::Type::String
# 左右の空白を除去した文字列クラス
#
# @param [Object] value
#
# @return [String, nil]
def cast(value)
super(value)&.strip&.presence
end
# 与えられた値をDBのクエリ用に変換する
#
# @param value [Object]
#
# @return [String, NilClass]
def serialize(value)
cast(value)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment