# Next Record

**URL:** <https://www.ruby-forum.com/t/next-record/95391>\
**Category:** Rails\
**Created:** [January 17, 2006, 7:26am UTC](https://www.ruby-forum.com/t/next-record/95391 "2006-01-17T07:26:48Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![rmasters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@rmasters](https://www.ruby-forum.com/u/rmasters)\
**Post date:** [January 17, 2006, 7:26am UTC](https://www.ruby-forum.com/t/next-record/95391/1 "2006-01-17T07:26:48Z")

</div>

Okay, I know this will probably be a very easy question. But I am  
discovering that RonR makes just about everything easy. The problem is,  
it takes me a few hours to figure out that I can do it the easy way.

I need to know how to move to the next record in a database. I want to  
format my out put in a table. For example, I want a 3 by 3 table. So  
record one goes in the first cell, record two goes in the middle top  
cell, etc.

Thanks.

-Ross

---

<div class="post-metadata">

**Author:** ![rmasters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@rmasters](https://www.ruby-forum.com/u/rmasters)\
**Post date:** [January 17, 2006, 9:02am UTC](https://www.ruby-forum.com/t/next-record/95391/2 "2006-01-17T09:02:45Z")

</div>

Ross:

controller

@record = Model.find(:all) – record could have been ‘x’ or anything  
else you like. :all can be limited using :conditions

in the view you put

@record.each do |r|

etc....

\<%end%\>

| header |
| --- |
| \<%=r.name%\> |

---

<div class="post-metadata">

**Author:** ![rmasters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@rmasters](https://www.ruby-forum.com/u/rmasters)\
**Post date:** [January 17, 2006, 7:55pm UTC](https://www.ruby-forum.com/t/next-record/95391/3 "2006-01-17T19:55:22Z")

</div>

Thanks for the reply, but that does not quite work. It puts everything  
in one row. But I need multiple rows and columns, like this:

Record 1 Record 2 Record 3

Record 4 Record 5 Record 6

Record 7 Record 8 Record 9

In ASP I would use a MoveNext method and 2 For…Next loops to handle  
this. So, what is the RonR equivalent of MoveNext?

Thanks.

-Ross

bruce balmer wrote:

> Ross:
> 
> controller
> 
> @record = Model.find(:all) – record could have been ‘x’ or anything  
> else you like. :all can be limited using :conditions
> 
> in the view you put
> 
> @record.each do |r|
> 
> etc....
> 
> \<%end%\>
> 
> | header |
> | --- |
> | \<%=r.name%\> |

---

<div class="post-metadata">

**Author:** ![rmasters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@rmasters](https://www.ruby-forum.com/u/rmasters)\
**Post date:** [January 17, 2006, 8:16pm UTC](https://www.ruby-forum.com/t/next-record/95391/4 "2006-01-17T20:16:52Z")

</div>

Well, how about this:

@x = Model.find(:all)

This produces an array of objects, one object for each row you will  
have pulled down.

in the cells respectively, I would place

Then each record you want would be in the appropriately numbered  
array element. ie. first record would be in @x[0], next record in @x[1]

So I’d write two loops, one to go sideways ([0] [1] [2]) and one  
to go vertically ie [0] [3] [6] et  
voilà.  
Two loops counting through the array elements where @x = the array.

bruce

---

<div class="post-metadata">

**Author:** ![rmasters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@rmasters](https://www.ruby-forum.com/u/rmasters)\
**Post date:** [January 17, 2006, 9:02pm UTC](https://www.ruby-forum.com/t/next-record/95391/5 "2006-01-17T21:02:07Z")

</div>

Something like this would work with just one loop. Making it  
prettier is left as an exercise for the reader.  
-Rob

\<% records = @Record.find(:all) per\_row = 3 records.each\_index { |position| if position.modulo(per\_row).zero? -%\> \<% end -%\> \<% if position.next.modulo(per\_row).zero? -%\> \<% end } if ! records.length.modulo(per\_row).zero? -%\> \<% end -%\>

| Record \<%= "#{position.next}: #{records[position].name}" %\> |

---

<div class="post-metadata">

**Author:** ![rmasters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@rmasters](https://www.ruby-forum.com/u/rmasters)\
**Post date:** [January 17, 2006, 9:02pm UTC](https://www.ruby-forum.com/t/next-record/95391/6 "2006-01-17T21:02:08Z")

</div>

This is ugly, but lets you change the # of columns very easily:

\<%

myCount = 0  
numColumns = 3

@records.each do |record|  
if myCount % numColumns == 0  
%\>

\<%  
end  
%\> \<%= record %\>  
if myCount == numColumns  
%\>\<%  
end  
end  
%\>

---

<div class="post-metadata">

**Author:** ![rmasters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@rmasters](https://www.ruby-forum.com/u/rmasters)\
**Post date:** [January 17, 2006, 9:05pm UTC](https://www.ruby-forum.com/t/next-record/95391/7 "2006-01-17T21:05:11Z")

</div>

Whoops… typo… make sure you increment the myCount variable before  
the  
end of the @records.each loop… sorry about that…

-Will

On 1/17/06, Will B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> %><tr><%
> 
> ```
> 
> end  
> %\> \<%= record %\>  
> if myCount == numColumns  
> %\>\<%  
> end

end

---

<div class="post-metadata">

**Author:** ![rmasters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@rmasters](https://www.ruby-forum.com/u/rmasters)\
**Post date:** [January 17, 2006, 8:53pm UTC](https://www.ruby-forum.com/t/next-record/95391/8 "2006-01-17T20:53:04Z")

</div>

On 1/17/06, Ross M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> In ASP I would use a MoveNext method and 2 For…Next loops to handle  
> this. So, what is the RonR equivalent of MoveNext?

Not sure if there is an equivalent, but something like this may work:

# For m columns (assuming records/length is cleanly divisible by m)

\<% 0.upto(@records.length/m-1) do |i| -%\>

 \<% 0.upto(m-1) do |j| -%\> \<%= @records[m\*i+j] %\> \<% end %\> \<% end %\>

---

<div class="post-metadata">

**Author:** ![rmasters](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@rmasters](https://www.ruby-forum.com/u/rmasters)\
**Post date:** [January 18, 2006, 12:15pm UTC](https://www.ruby-forum.com/t/next-record/95391/9 "2006-01-18T12:15:22Z")

</div>

Ross M. wrote:

> In ASP I would use a MoveNext method and 2 For…Next loops to handle  
> this. So, what is the RonR equivalent of MoveNext?

You could use each\_index, but I prefer to shift elements off lists  
because it’s so much more readable:

 \<% while @records.length \> 0 -%\> \<% 3.times do -%\> \<% end -%\> \<% end -%\>

| \<%= @records.shift.name %\> |
