LoginSignup
3
4

More than 5 years have passed since last update.

年齢を○代前半/後半に変更したい

Last updated at Posted at 2015-04-22

以外となさそうだったので。

前提

Userというmodelにメソッドを追加したイメージ。
Userageってカラムを持ってる。

コード

User.rb

  def round_to_generation
    "#{ age / 10 * 10 }#{ age.round(-1) == age / 10 * 10 ? '前' : '後' }半"
  end

結果

# user.age = 24のとき
user.round_to_generation # => "20代前半"


# user.age = 37のとき
user.round_to_generation # => "30代後半"

ってなる。

3
4
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4