Both with_index and each_with_index not working

  • @reports.to_enum.with_index(1).each do |r, i|
    - r.alerts.reverse.each do |a|
    - if a.code == 14
    %tr
    %td.num_col Trip #{i}
    %td.start_col #{r.time}
    %td.end_col
    %td.distance_col #{0}

i should equal 1 during first iteration. However, it is equal to 20
during every iteration. Not sure what’s going on.

thanks for response

Maybe the title of this post sounds crazy but its true.

  • iterate_for(@reports)
  • @reports.to_enum.with_index(1).each do |r, i|
    - r.alerts.reverse.each do |a|
    - if a.code == 14
    %tr
    %td.num_col Trip #{i}
    - elsif a.code == 15
    %tr
    %td.num_col Trip #{i}

def iterate_for(reports)
Rails.logger.info “THe value of reports is #{reports}”
Rails.logger.info “The length of reports i #{reports.size}”
reports.to_enum.with_index(1).each do |r,i|
Rails.logger.info “The report is #{r}”
Rails.logger.info “The count is #{i}”
end
end

I created an iterate_for method just to see what happens when I log
the output in a helper to the console. And the with_index method
correctly sets the value of i to 1 and so forth. But within the haml,
this:

Trip #{i}

i is equal to the value 20 every time.

Im not sure what’s going on.

thanks for response

After typing second message, I realized the nested loop was causing
the issue.