我正在尝试从 mysql 数据库中的离开表中计算特定学生“UG10001”在特定月份所采取的休假。我正在使用以下代码片段-
def calculate(student_id)
leave = Leave.find_by_sql("SELECT leave_duration FROM leaves WHERE MONTH(leave_from) = 2
AND YEAR(leave_from) = 2013 AND MONTH(leave_to) = 2 AND YEAR(leave_to) = 2013 AND
academic_status = 'APPROVED' AND warden_status = 'APPROVED' AND student_id = student_id
AND status = 'ACTIVE'")
sum = leave.sum(&:leave_duration)
return sum
end
- - - - - 更新 - - - - -
def calculate(student_id)
sum = Leave.find_by_sql("SELECT leave_duration FROM leaves WHERE MONTH(leave_from) = 2
AND YEAR(leave_from) = 2013 AND MONTH(leave_to) = 2 AND YEAR(leave_to) = 2013 AND
academic_status = 'APPROVED' AND warden_status = 'APPROVED' AND student_id = student_id
AND status = 'ACTIVE'").sum(&:leave_duration)
return sum
#it did the trick for me.
end
上述方法将计算特定学生在 2013 年 2 月的休假,并将休假总和返回给调用函数。
def calcuateLeaveForEachStudent
array = Leave.find_by_sql("select student_id from students")
c = Array.new
for i in 0...array.length
student_id = array[i].student_id
c[i] = calculate(student_id)
end
end
但是当我调用上述方法时,'calculate' 方法在每次迭代中返回相同的值。可能的解决方案是什么..?提前致谢..!
PS -
代码在 Rails 控制台上完美运行,没有任何语法错误,但是有一些我无法弄清楚的错误。