开发者

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!


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜