Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@benmezger
Forked from aukevs/gist:2169369
Created October 20, 2012 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benmezger/3923484 to your computer and use it in GitHub Desktop.
Save benmezger/3923484 to your computer and use it in GitHub Desktop.
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