Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 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 -%>
Add Comment
Please, Sign In to add comment