I think there is a very good chance that what you are doing is trying to work against the way Rails works rather than with it. While there’s nothing to stop you doing that, I am confident you will be much more productive working with Rails rather than against it.
Before I used Rails, I used row indexes a lot. That was mainly in ASP classic. The row index was the best flag for where I was when iterating through a group of items - particularly rows of data retrieved from a database.
Since I started using Rails I’ve never found the need to get the row index. It was only reading your post that made me think about what I used to do and why I don’t need them any more.
Row indexes were useful to get information that related to a particular row of data. So functions would behave in a way where the row number would be passed to the function to return a parameter. So if you wanted the colour of the data at row 157, you’d do something along the lines of get_colour_for_item(157), where get_colour_for_item needed to know about the way the records were arranged in a dataset (by passing it all the rows for example) and how to pick out the data at the given row and process it to retrieve the correct output. You’d do something like this (in generic pseudo code):
for row_number = 1 to number_of_rows_in(rows)
print get_colour_for_item(rows, row_number)
end
The key point being that the data in the rows is dumb and the function has to work on the whole set of rows, be able to iterate through them and pull out the right one, as well as being able to return the correct output.
With Rails things don’t work that way. The key point is that Rails does not return rows of data. Instead it returns arrays of objects. Each object encapsulates a group of methods that act on the data stored within the object (and just that object) to return desired output.
With Rails the functions (called methods in Ruby) act just on the single lump of data within each object. They don’t need to find the data because it is already attached. They just need to know how to transform the data into the correct output format.
So with Rails you grab the object and tell the object itself to call its own local method. You don’t need row numbers because the object doesn’t need to know where it is within a data set to be able to act on a request. The Rails equivalent of my pseudo code above would be something like this:
for object in objects
puts object.colour
end
Importantly the code that grabs the data and separates it into individual pieces is separated from the code that uses that data to return a particular output. In my Rails example, the method ‘colour’ doesn’t need to know how to find the object’s data within the dataset. All it needs to know is how to convert the object’s data into the correct output.
The code that separates the data into a set of individual datum objects is that which generates the array of objects. Its already done for you. Which means that all the developer needs to do is write the code that operates at the object level. That’s a lot simpler and a lot cleaner. The result is better coding.
You can force Rails to work with row numbers if you want, but in five years of Rails coding I’ve never found a need to.