how to access index of my collection object?

i have something like

@a = Model.find(:all)

for a in @a
  here i need to know what is my current index / row
end

how do i get it?.

i have something like

@a = Model.find(:all)

for a in @a
here i need to know what is my current index / row
end

I would use each_with_index if I were you.

Fred

Three suggestions:

1. The index is going to be somewhat arbitrary unless you get the records in a well-defined order (i.e., put an :order => 'column_a, column_b' option into your find). (Or use a named_scope or some class method on your model rather than letting your controller do the work.)

2. If you want the current index to do something like zebra-striping table rows, look at the cycle() helper method for your view.

3. Rather than "for a in @a", you might want "@a.each do |a|" as James Edward Gray II explains in "The Evils of the For Loop"
    http://blog.grayproductions.net/articles/the_evils_of_the_for_loop

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com