Rails,二维表,pivot,嵌套哈希循环

时间:2021-07-02 21:36:20

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.

我考虑过使用ruport / acts_as_reportable或mysql pivot程序,但看起来这个pivot只给了我一个维度。所以这不会起作用,因为在我看来,我想添加鼠标悬停功能和条件格式,以显示每个年级的更多信息。

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)?

所以我认为我唯一的选择是生成一个嵌套的哈希,然后在视图中循环它。你怎么看?有人可以建议一种方法来构建嵌套哈希吗?循环250行(约50名学生,每门5堂课)是否过于耗费处理器?

I am stuck. Please help. Thanks!

我被卡住了。请帮忙。谢谢!

1 个解决方案

#1


3  

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

#1


3  

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