我在局部顯示一個學生各評價期間的平均得分的圖以下代碼:打印一個空表單元格,如果方法沒有返回數據
<tr class="<%= cycle("odd", "even", name: "students")%>">
<td>
<%= link_to "#{student.name}",
student_path({student_group_id: student.student_group_id, id: student.id})%>
</td>
<% student.eval_count.times do |i| %>
<td class="center"><%= student.avg_for_eval(i) %></td>
<% end %>
<td class="center"><%= student.avg unless student.avg.nan? %></td>
</tr>
我使用在student.rb
此方法生成平均分數,起初我無法弄清楚爲什麼我無法在沒有數據時無法生成空行。
def evals
evals = self.evaluations.order("eval_number").group_by(&:eval_number)
end
def eval_number_set(index)
numbers = Evaluation.where('student_id = ?', self.id).uniq.pluck(:eval_number)
numbers[index]
end
def avg_for_eval(i)
scores = []
evals = self.evals.select { |k, v| k == self.eval_number_set(i) }.values.first
for eval in evals
scores << eval.score
end
evals.empty? #(scores.sum.to_f/scores.size).round(2)
end
我改變了方法的最後一行evals.empty?
如上圖所示,並得到了這個瀏覽器:
然後我意識到,因爲select
任何評價學生不存在(對於新學生)不是我用來生成代碼的數據集的一部分。
從圖中可以看出,問題在於,只有最後一次評估數據的兩名學生沒有將他們的數據輸入到正確的列中 - 所有內容都向左移動,因爲沒有<td>
由視圖代碼產生。
所以問題的話,是如何可以重寫方法的代碼,使得我得到同樣的輸出,但使得<td>
在視圖印刷if scores.empty?
的方法插入""
或"no data"
或一些其它的佔位符?
更新
我明白現在好了一點。這種方法:
def eval_number_set(index)
numbers = Evaluation.where('student_id = ?', self.id).uniq.pluck(:eval_number)
numbers[index]
end
是返回評價號碼每個學生 - 因此,當這些數字在未來的方法是使用
def avg_for_eval(i)
scores = []
evals = self.evals.select { |k, v| k == self.eval_number_set(i) }.values.first
for eval in evals
scores << eval.score
end
evals.empty? #(scores.sum.to_f/scores.size).round(2)
end
只能拉一個學生就一直存在評價對於。回到繪圖板現在...
更新2
我已經改變了類的方法如下:
#returns all 'eval_number's for a given group of students
#as the first student will have been present for all evaluations
def eval_number_set(index)
numbers = self.student_group.students.first.evals.keys
# numbers = Evaluation.where('student_id = ?', self.id).uniq.pluck(:eval_number)
numbers[index]
end
#attempts to match the first present 'eval_number' for a given student against
#the first number in the set of all 'eval_number's and react accordingly
def avg_for_eval(i)
scores = []
if self.evals.keys[i] == self.eval_number_set(i)
"match"
else
"no_match"
end
end
此匹配的所有誰是目前每一個評價學生,但對錯過了一些學生的學生沒有任何評價。我改變了代碼以下
def avg_for_eval(i)
scores = []
if self.evals.keys[i] == self.eval_number_set(i)
"#{self.evals.keys[i]} vs #{self.eval_number_set(i)}"
else
"#{self.evals.keys[i]} vs #{self.eval_number_set(i)}"
end
end
並且退回它在瀏覽器中執行以下操作:
所以我試圖在添加計數器if語句,例如,如果語句匹配,這將增加正在嘗試的關鍵,否則會留在同一個鍵:
def avg_for_eval(i)
scores = []
key_match = 0
if self.evals.keys[key_match] == self.eval_number_set(i)
"#{self.evals.keys[i]} vs #{self.eval_number_set(i)}"
key_match += 1
else
"#{self.evals.keys[i]} vs #{self.eval_number_set(i)}"
end
end
產生這樣的:
和有意義 - 計數遞增不走的,因爲它被調用的方式任何實際效果。但是當我認爲我現在更接近我想要的效果時,我不知道如何讓它發生!
更新3
接近還是......我已經改變了,這樣的鍵反向閱讀,因此,所有的數據都被讀 - 但我還是很喜歡的數據是打印與最近的右邊(見下圖)。該模型方法的代碼現在看起來是這樣的:
def eval_number_set(index)
numbers = self.student_group.students.first.evals.keys.reverse
# numbers = Evaluation.where('student_id = ?', self.id).uniq.pluck(:eval_number)
numbers[index]
end
def avg_for_eval(i)
scores = []
eval_number = self.eval_number_set(i)
if self.evals.keys.reverse[i] == eval_number
for eval in self.evals.values[i]
scores << eval.score if self.evals.values[i]
end
scores
else
"no data"
end
end
返回;下面,我已經註釋,所以你可以理解它是什麼,我找了一下更清楚:
更新4
調用student.evals
返回以下(學生與id
32):
{29=>[
#<Evaluation id: 1949, score: 3, created_at: "2013-08-28 09:44:32", updated_at: "2013-08-28 09:44:32", student_id: 32, goal_id: 63, eval_number: 29>,
#<Evaluation id: 1950, score: 4, created_at: "2013-08-28 09:44:32", updated_at: "2013-08-28 09:44:32", student_id: 32, goal_id: 64, eval_number: 29>,
#<Evaluation id: 1951, score: 5, created_at: "2013-08-28 09:44:32", updated_at: "2013-08-28 09:44:32", student_id: 32, goal_id: 65, eval_number: 29>],
30=>[
#<Evaluation id: 1957, score: 3, created_at: "2013-08-28 09:44:43", updated_at: "2013-08-28 09:44:43", student_id: 32, goal_id: 65, eval_number: 30>,
#<Evaluation id: 1956, score: 2, created_at: "2013-08-28 09:44:43", updated_at: "2013-08-28 09:44:43", student_id: 32, goal_id: 64, eval_number: 30>,
#<Evaluation id: 1955, score: 1, created_at: "2013-08-28 09:44:43", updated_at: "2013-08-28 09:44:43", student_id: 32, goal_id: 63, eval_number: 30>],
31=>[
#<Evaluation id: 1968, score: 2, created_at: "2013-08-28 11:26:56", updated_at: "2013-08-28 11:26:56", student_id: 32, goal_id: 70, eval_number: 31>,
#<Evaluation id: 1967, score: 2, created_at: "2013-08-28 11:26:56", updated_at: "2013-08-28 11:26:56", student_id: 32, goal_id: 69, eval_number: 31>,
#<Evaluation id: 1966, score: 1, created_at: "2013-08-28 11:26:56", updated_at: "2013-08-28 11:26:56", student_id: 32, goal_id: 68, eval_number: 31>,
#<Evaluation id: 1965, score: 1, created_at: "2013-08-28 11:26:56", updated_at: "2013-08-28 11:26:56", student_id: 32, goal_id: 67, eval_number: 31>,
#<Evaluation id: 1964, score: 1, created_at: "2013-08-28 11:26:56", updated_at: "2013-08-28 11:26:56", student_id: 32, goal_id: 66, eval_number: 31>,
#<Evaluation id: 1963, score: 3, created_at: "2013-08-28 11:26:56", updated_at: "2013-08-28 11:26:56", student_id: 32, goal_id: 65, eval_number: 31>,
#<Evaluation id: 1962, score: 3, created_at: "2013-08-28 11:26:56", updated_at: "2013-08-28 11:26:56", student_id: 32, goal_id: 64, eval_number: 31>,
#<Evaluation id: 1961, score: 3, created_at: "2013-08-28 11:26:56", updated_at: "2013-08-28 11:26:56", student_id: 32, goal_id: 63, eval_number: 31>],
32=>[
#<Evaluation id: 1983, score: 3, created_at: "2013-08-29 19:31:48", updated_at: "2013-08-29 19:31:48", student_id: 32, goal_id: 69, eval_number: 32>,
#<Evaluation id: 1982, score: 2, created_at: "2013-08-29 19:31:48", updated_at: "2013-08-29 19:31:48", student_id: 32, goal_id: 68, eval_number: 32>,
#<Evaluation id: 1981, score: 3, created_at: "2013-08-29 19:31:48", updated_at: "2013-08-29 19:31:48", student_id: 32, goal_id: 67, eval_number: 32>,
#<Evaluation id: 1980, score: 4, created_at: "2013-08-29 19:31:48", updated_at: "2013-08-29 19:31:48", student_id: 32, goal_id: 66, eval_number: 32>,
#<Evaluation id: 1979, score: 4, created_at: "2013-08-29 19:31:48", updated_at: "2013-08-29 19:31:48", student_id: 32, goal_id: 65, eval_number: 32>,
#<Evaluation id: 1978, score: 3, created_at: "2013-08-29 19:31:48", updated_at: "2013-08-29 19:31:48", student_id: 32, goal_id: 64, eval_number: 32>,
#<Evaluation id: 1977, score: 3, created_at: "2013-08-29 19:31:48", updated_at: "2013-08-29 19:31:48", student_id: 32, goal_id: 63, eval_number: 32>,
#<Evaluation id: 1984, score: 3, created_at: "2013-08-29 19:31:48", updated_at: "2013-08-29 19:31:48", student_id: 32, goal_id: 70, eval_number: 32>]
}
「link_to」#{student.name}「,student_path(...)」。這條線可能會重寫爲「link_to student.name,student_path(...)」 – hedgesky
啊很好的捕獲,不知道爲什麼就是這樣的 – dax
會像'def avg_for_eval(index) \t Evaluation.where('student_id =?和eval_number =?',self.id,index).sum() end' work? – Gonfva