User Model Basics: Age Calculations
Join the DZone community and get the full member experience.Join For Free
class User < ActiveRecord::Base # ... def age (Time.now.year - birthday.year) - (turned_older? ? 0 : 1) rescue 0 end def next_birthday birthday.to_time.change(:year => (turned_older? ? 1.year.from_now : Time.now).year) end def turned_older? (birthday.to_time.change(:year => Time.now.year) <= Time.now) end end
Opinions expressed by DZone contributors are their own.