In my rails app, i have the following function in one of my controller page.
def tree
@jstree = Sub.find_by_sql("Select sequence, depth, node, imageid, range from.....several joins")
end
I now want to loop through the resultset and perform some actions depending on the value of the prev and next row.
<% @jstree.each do |tree| %>
<%= tree.sequence %>
<% end %>
I would like to know how to get the next and previous row value within the above loop
Many many thanks for all suggestions provided 🙂
It depends on how you actually need to use it (and obviously the first and last records won’t have previous/next elements).
If you do an
each_with_indexyou have the index and can use normal array access: