Current row value in with each method

So i’m coming from a coldfusion experience background and when dumping db results to screen there is a method called ‘current_row’ that will give me the index number of the current record.

Is there something already built into ruby or rails that does the same thing or is this something I have to write myself?

From the description you provided, it almost sounds like a problem better solved with JavaScript and CSS. You could model a stylesheet much like the guys from blueprintcss.org, but instead dividing things into columns you would be dividing things into rows. Use JavaScript to add the appropriate class, which would give you your vertical positioning and z-index.

I think the simplest solution would be to store the increment (that you are currently using row number for) as a class variable, and increment it each time the method runs.

So you could do something like this:


@@top = 10

def timeline_event(event, timeline_start_date)
  
  @@top += 35

  ....... rest of you code replacing top with @@top

end

what i’m trying to accomplish has nothing to do with a method or any aspect of the model. What I needed the current_row number for was for positioning (and z-index).

So i’ve got these rows (tasks) and they’re being put into a gantt chart. The current_row number tells me how many times to multiply the height of the current object by to get it in the correct position on the screen and what the z-index should be to prevent unwanted overlaps with other screen elements.

Here’s my helper, I’m still pretty ‘noobish’ with ruby AND rails, so if there’s a more efficient method or concept, please tell.


  def timeline_event(current_row, event, timeline_start_date)
    top = (current_row*35)+10

 	  if event.due_date < timeline_start_date
 	    left = 0
 	    width = 700
 	    background = "#770000"
     else
    	 left = (event.start_date - timeline_start_date)*262
   	   width = (Task.days_in_range(event.start_date..event.due_date)*262)-131
   	   if left < 0
    	   left_offset = left
    	   left = 0
    	   width = width + left_offset
  	   end
       background = cycle("#002f54","#510054","#bf4c18","#528a74","#918b16","#254000")
     end
    
    content_tag(:div, :class => 'timeline_task', :style => "top:#{top}px;left:#{left}px;width:#{width}px;background-color:#{background};height:23px;z-index:#{100-current_row}") do
      content_tag(:span, "#{event.start_date.strftime('%m/%d/%Y')} - #{event.due_date.strftime('%m/%d/%Y')} | #{event.task} #{event.class}", :title => "#{event.owner.whole_name} | #{event.task}")
    end
  end

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.

yes and no. Thanks for the response. I’ll have to read about the each_with_index. I guess what I was really wanting was my .current_row method from CF… sigh. :slight_smile:

I’m okay with the partial_count solution… for now.

If you are just wanting the index returned you can use the Array#each_index method that passes the index of the array instead of the object of the array to the block. There is also the Enumerable#each_with_index method that takes the object and the index in the block.

a =
a.each_with_index {|object, index| code…}

Is this what you were looking for?

okay, so i finally accepted it doesn’t exist BUT found the _counter method for partials. Turned my each method into a partial loop and i’m done. For anyone else with the same problem.

render(:partial => ‘books’, :collection => @book_list)
“books_counter” method will give you the current index.