Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Semester < ApplicationRecord
- has_and_belongs_to_many :students
- accepts_nested_attributes_for :students
- end
- class Student < ApplicationRecord
- has_and_belongs_to_many :semesters
- has_many :attendances, dependent: :destroy
- accepts_nested_attributes_for :attendances
- end
- class Attendance < ApplicationRecord
- belongs_to :semester
- belongs_to :student
- validates_presence_of :date
- end
- @semester.students.each do |student|
- student.attendances
- end
- # semesters_controller.rb
- def show
- @semester = Semester.includes(students: [:attendances])
- .order('students.first_name')
- .find params[:id]
- end
- # students_helper.rb
- def student_attendance(student)
- total = student.attendances.select { |x| x.semester_id == @semester.id }
- present = total.select &:present
- percent = (present.size/total.size.to_f * 100).round rescue 0
- link_to student, class: 'attendance', style: "width: #{percent}%" do
- <<-HTML.html_safe
- <span>#{student.first_name}</span>
- <span>#{percent}%</span>
- HTML
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement