Guest User

Untitled

a guest
Sep 18th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. # student.rb
  2. class Student
  3. def age
  4. unless read_attribute(:birthdate) == nil
  5. dob = read_attribute(:birthdate)
  6. now = Time.now.utc.to_date
  7. now.year - dob.year - ((now.month > dob.month || (now.month == dob.month && now.day >= dob.day)) ? 0 : 1)
  8. else
  9. 0
  10. end
  11. end
  12. end
  13.  
  14. # controllers/students_controller.rb
  15. class StudentsController < ActionController
  16. def show
  17. @student = Student.find(params[:id])
  18. end
  19. end
  20.  
  21. # views/show.html.erb
  22. <% if @student.birthday.to_date == Time.now.to_date %>
  23. <h1><%= "Happy #{@student.age}th birthday #{@student.name}" %></h1>
  24. <% end -%>
Add Comment
Please, Sign In to add comment