Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails, two dimensional table, pivot, nested hash loops

I am building grade-book report - a two dimensional table that shows lesson names going horizontally and a list of students going vertically.

Student Name | LessonID x | LessonID x | LessonID x          
Joe                 95%        95%
Mary                80%        80% 
Sam                 80%                    80%

My data is in a table that has these fields:

student_id, lesson_id, grade_in_pct, grade_in_pts, grade_high, grade_low, grade_median

The total number of students and lessons is not fixed.

I considered using ruport/acts_as_reportable or mysql pivot procedure, however it looks like the pivot only gives me one dimension. So that's not going to work, because in my view I want to add mouse-over features and conditional formatting to show more info on each grade.

So I think my only option is to generate a nested hash and then loop through it in the view. What are your thoughts? Could someone suggest a way to build a nested hash? Would it be too processor intensive to loop through 250 rows (~50 students, 5 lessons each)?

I am stuck. Please help. Thanks!

like image 580
Alex Avatar asked Nov 17 '10 17:11

Alex


1 Answers

This is how I would do it:

MODELS:

Student Model:
  has_many: Grades
  has_and_belongs_to_many: Lessons

Lesson Model:
  has_many: Grades
  has_and_belongs_to_many: Students

Grade Model:
  belongs_to: Student, Lesson

CONTROLLER:

@data = Student.all
@lessons = Lesson.all

VIEW:

header_row

@data.each do |student|
  @lessons.each do |lesson|
    student.grades.find_by_lesson(lesson).some_data
like image 101
thomasfedb Avatar answered Sep 30 '22 16:09

thomasfedb