を最適化Iは、以下の少なくとも18レール:条件場合メソッド
def person_age
if person_birthdate.present?
now = Time.now.utc.to_date
begin
parsedDate = Date.parse(person_birthdate, '%d/%m/%Y')
diff = now.year - parsedDate.year
diff -= (diff.years.since(parsedDate) > now ? 1 : 0)
if diff < 18
errors.add :person_birthdate, 'You should be at least 18'
end
rescue
errors.add :person_birthdate, 'Date not valid'
end
else
errors.add :person_birthdate, 'Date not valid'
end
end
あること(「%のD /%M /%Y」の形式で)ユーザの誕生日をチェックする方法しかし、あまりにも多くを持っていますどのようにそれをより良く見えるようにするための任意のアイデア?
それはそうです。どうしてそれをするの?それが日付の列だった場合、Railsはあなたのために解析を処理し、あなたの検証ははるかに簡単になります。 – spickermann