Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. class Semester < ApplicationRecord
  2. has_and_belongs_to_many :students
  3. accepts_nested_attributes_for :students
  4. end
  5.  
  6. class Student < ApplicationRecord
  7. has_and_belongs_to_many :semesters
  8. has_many :attendances, dependent: :destroy
  9. accepts_nested_attributes_for :attendances
  10. end
  11.  
  12. class Attendance < ApplicationRecord
  13. belongs_to :semester
  14. belongs_to :student
  15. validates_presence_of :date
  16. end
  17.  
  18. @semester.students.each do |student|
  19. student.attendances
  20. end
  21.  
  22. # semesters_controller.rb
  23. def show
  24. @semester = Semester.includes(students: [:attendances])
  25. .order('students.first_name')
  26. .find params[:id]
  27. end
  28.  
  29. # students_helper.rb
  30. def student_attendance(student)
  31. total = student.attendances.select { |x| x.semester_id == @semester.id }
  32. present = total.select &:present
  33. percent = (present.size/total.size.to_f * 100).round rescue 0
  34. link_to student, class: 'attendance', style: "width: #{percent}%" do
  35. <<-HTML.html_safe
  36. <span>#{student.first_name}</span>
  37. <span>#{percent}%</span>
  38. HTML
  39. end
  40. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement