问题
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.)
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
I can easily show the labels across either the x OR the y but not both.
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) 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...
Any and all suggestions are welcome!
回答1:
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
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
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.
来源:https://stackoverflow.com/questions/12691393/rails-presenting-complex-data-view