Forum: Ruby on Rails Next Record

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ross M. (Guest)
on 2006-01-17 08:26
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
Bruce B. (Guest)
on 2006-01-17 10:02
(Received via mailing list)
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

<table>
<tr>
<th> header </th>


@record.each do |r|
<tr>
<td><%=r.name%></td>
etc....

</tr>

<%end%>
</table>
Ross M. (Guest)
on 2006-01-17 20:55
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
>
> <table>
> <tr>
> <th> header </th>
>
>
> @record.each do |r|
> <tr>
> <td><%=r.name%></td>
> etc....
>
> </tr>
>
> <%end%>
> </table>
Bruce B. (Guest)
on 2006-01-17 21:16
(Received via mailing list)
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
Jeremy E. (Guest)
on 2006-01-17 21:53
(Received via mailing list)
On 1/17/06, Ross M. <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| -%>
  <tr>
  <% 0.upto(m-1) do |j| -%>
     <td><%= @records[m*i+j] %></td>
  <% end %>
  </tr>
<% end %>
Rob B. (Guest)
on 2006-01-17 22:02
(Received via mailing list)
Something like this would work with just one loop.  Making it
prettier is left as an exercise for the reader.
-Rob


<table>
<% records = @Record.find(:all)
    per_row = 3
    records.each_index { |position|
         if position.modulo(per_row).zero?  -%>
<tr>
<%      end -%>
<td>Record <%= "#{position.next}: #{records[position].name}" %></td>
<%      if position.next.modulo(per_row).zero? -%>
</tr>
<%      end
    }
    if  ! records.length.modulo(per_row).zero? -%>
</tr>
<% end -%>
</table>
Will B. (Guest)
on 2006-01-17 22:02
(Received via mailing list)
This is ugly, but lets you change the # of columns very easily:

<%

myCount = 0
numColumns = 3

@records.each do |record|
  if myCount % numColumns == 0
    %><tr><%
  end
  %><td> <%= record %></td>
  if myCount == numColumns
    %></tr><%
  end
end
%>
Will B. (Guest)
on 2006-01-17 22:05
(Received via mailing list)
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> wrote:
>     %><tr><%
>   end
>   %><td> <%= record %></td>
>   if myCount == numColumns
>     %></tr><%
>   end
>
end
Alex Y. (Guest)
on 2006-01-18 13:15
(Received via mailing list)
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:

<table>
   <% while @records.length > 0 -%>
     <tr>
       <% 3.times do -%>
         <td><%= @records.shift.name %></td>
       <% end -%>
     </tr>
   <% end -%>
</table>
This topic is locked and can not be replied to.