Topic: for loop mysteriously not executing

Here is the the that is not executing for some reason:

def sum_order_by_type(type, order)
    sum= 0
    for line_item in order.finished_line_items
        #sum = 0/0
      if line_item.typeID == type.id
        sum += line_item.qty
      end
    end
    #sum= order.finished_line_items.count
    sum
  end

The strange thing is that if I uncomment the two commented lines, a divide by zero error does not occur, meaning that the for loop has not been iterated, but, order.finished_line_items.count returns the correct amount! I cannot figure out why the loop will not iterate.

Re: for loop mysteriously not executing

I've solved my own problem by adding :refresh to the for loop like this:

for line_item in order.finished_line_items(:refresh)