Rails - 呈现复杂的数据视图 [英] Rails - Presenting complex data view

查看:34
本文介绍了Rails - 呈现复杂的数据视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该是一些简单的事情,但我已经研究了一天,看不到明确的答案.

This should be something simple but I've been at it for a day now and can't see a clear answer.

我有三个基本模型:用户、评估和工作.用户在作业上工作,作业所有者对用户进行评估.一个用户可以有许多不同的工作.每个工作都会对其相关用户进行多次评估(在工作中,每个相关用户都会有相同的评估——当然,得分不同.)

I have three basic models: User, Evaluation and Job. Users work on the job and the job owner evaluates the users. A user can have many various jobs. Each job will evaluate its related users multiple times (within the job, each related user will have the same evaluations - with different scores, of course.)

所以,简单地说,我想以表格形式(有点像 Excel 工作簿)显示数据,其中评估名称沿 x 轴排列,用户名沿 y 轴排列.

So, simply, I would like to present the data in tabular form (kind of like an excel workbook) with the evaluation name going across the x axis and the user names going down the y axis.

所以,它看起来像这样:

So, it would look like this:

        Evaluation 1  |  Evaluation 2  |  Evaluation 3
       _______________________________________________________
User 1   Good              Poor            Outstanding
User 2   Poor              Good            Good

我可以轻松地在 x 或 y 上显示标签,但不能同时显示两者.

I can easily show the labels across either the x OR the y but not both.

Q1) 每个工作/用户评估都是它自己的数据库记录.关于如何在 x 和 y 轴上构建标签的任何建议?

Q1) Each Job/User Evaluation is its own db record. Any suggestions as to how I would structure the the LABELS across the x and y axis?

Q2) 我如何确保数据(例如,用户 2 评估 3)被正确分类(绘制)?并且,如果新用户(例如,用户 3)加入工作较晚并且没有评估 1 或评估 2...

Q2) How would I ensure the data (for example, User 2 Evaluation 3) is categorized (plotted) correctly? And, what if a new User (e.g., User 3) joins the job late and doesn't have Evaluation 1 or Evaluation 2...

欢迎提出任何建议!

推荐答案

一种方法是创建一个散列数组,每个散列包含用户评分和所有可用评估的数组,类似于

One way of doing it is to create an array of hashes, each hash containing user rating and an array of all available evaluations, something like

Evaluation.where(:job_id => 1)order('title').map{|e| e.title}.uniq

这会给你类似于 all_evaluations 的东西:

This will give you something similar to all_evaluations:

all_evaluations = ['eval1', 'eval2', 'eval3', 'eval4']

用户评估哈希看起来像这样:

User evaluations hash would look something like this:

user_evaluations = {"UserA"=>{:eval1=>1, :eval3=>3}}, {"UserB"=>{:eval1=>1, :eval=>3}}

all_evaluations 将用于标题行标签和为每个用户记录提取评估:

all_evaluations will be used for header row labels and for pulling evals for each user records:

<tr>
<% all_evaluations.each do |eval_header| %>
  <td><%= eval_header %></td>
<% end %>
</tr>

<% user_evaluations.each do |user| %>
  <tr>
    <td><%= user.keys %></td>
    <% all_evaluations.each do |eval| %>
      <td><%= user.values.first[eval.to_sym] %></td>
    <% end %>
  </tr>
end

此处可能存在一些错误/更改,但原则上应该可以正常工作.

There might be few bugs/changes here, but it should work in principal.

这篇关于Rails - 呈现复杂的数据视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆