Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Happy Birthday Ben
# student.rb
class Student
def age
unless read_attribute(:birthdate) == nil
dob = read_attribute(:birthdate)
now = Time.now.utc.to_date
now.year - dob.year - ((now.month > dob.month || (now.month == dob.month && now.day >= dob.day)) ? 0 : 1)
else
0
end
end
end
# controllers/students_controller.rb
class StudentsController < ActionController
def show
@student = Student.find(params[:id])
end
end
# views/show.html.erb
<% if @student.birthday.to_date == Time.now.to_date %>
<h1><%= "Happy #{@student.age}th birthday #{@student.name}" %></h1>
<% end -%>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.