Ruby on rails 3 Rails:在循环中调用函数会为每次迭代返回相同的值

Ruby on rails 3 Rails:在循环中调用函数会为每次迭代返回相同的值,ruby-on-rails-3,loops,methods,ruby-on-rails-3.2,Ruby On Rails 3,Loops,Methods,Ruby On Rails 3.2,我试图从mysql数据库中的leaves表中计算特定学生“UG10001”在特定月份的休假。我正在使用以下代码片段-- ----------更新---------- 上面的方法将计算某个学生在2013年2月的假期,并将假期的总和返回给调用函数 def calcuateLeaveForEachStudent array = Leave.find_by_sql("select student_id from students") c = Array.new for i in 0...ar

我试图从mysql数据库中的leaves表中计算特定学生“UG10001”在特定月份的休假。我正在使用以下代码片段--

----------更新----------

上面的方法将计算某个学生在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控制台上完美运行,没有任何语法错误,但是有一些错误我无法理解

 calculate(operation, column_name, options = {}).

或者您可以参考正确的方法是calculate(操作,列名称,options={})。您的方法类似于计算(sum,student_id)更好的选项是使用find with options,而不是find by_sql@Sachiner你能详细说明一下吗。。?我就是不明白@SahilGrover首先,我从students表中检索所有学生id,然后计算他们在特定月份的相应假期。@ph3n0m\u aks尝试类似于此假期的方法。其中(:academic\u status=>“APPROVED”、:warden\u status=>“APPROVED”、:student\u id=>student\u id……)
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(operation, column_name, options = {}).