Rails、二维表、枢轴、嵌套哈希循环

发布于 2024-10-02 19:14:43 字数 656 浏览 2 评论 0原文

我正在构建成绩册报告 - 一个二维表格,水平显示课程名称,垂直显示学生列表。

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

我的数据位于包含以下字段的表中:

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

学生和课程的总数不固定。

我考虑过使用 ruport/acts_as_reportable 或 mysql 数据透视程序,但看起来数据透视表只给了我一维。所以这是行不通的,因为在我看来,我想添加鼠标悬停功能和条件格式以显示每个成绩的更多信息。

所以我认为我唯一的选择是生成一个嵌套哈希,然后在视图中循环它。你有什么想法?有人可以建议一种构建嵌套哈希的方法吗?循环遍历 250 行(约 50 名学生,每人 5 节课)是否会导致处理器过于密集?

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

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隔岸观火 2024-10-09 19:14:43

我就是这样做的:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文