# How to do this?

**URL:** <https://www.ruby-forum.com/t/how-to-do-this/72438>\
**Category:** Rails\
**Created:** [October 17, 2006, 9:56am UTC](https://www.ruby-forum.com/t/how-to-do-this/72438 "2006-10-17T09:56:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Petr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ac8455/32.png) [@Petr](https://www.ruby-forum.com/u/Petr)\
**Post date:** [October 17, 2006, 9:56am UTC](https://www.ruby-forum.com/t/how-to-do-this/72438/1 "2006-10-17T09:56:16Z")

</div>

Hi,  
I’d like iterate through a model object(its an array object as you know)  
in the view so that it displays 2 database table rows in one html table  
row(in separate columns

) and then moves to display the next 2  
database table rows in the next html table row.

Like this:

…

| array[0] | array[1] |
|
| |
| array[2] | array[3] |
|
| |

Im kinda clueless about this. Any ideas?  
Thanks

Petr

---

<div class="post-metadata">

**Author:** ![Petr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ac8455/32.png) [@Petr](https://www.ruby-forum.com/u/Petr)\
**Post date:** [October 17, 2006, 4:07pm UTC](https://www.ruby-forum.com/t/how-to-do-this/72438/2 "2006-10-17T16:07:03Z")

</div>

Thanks.

But, you forgot to mention how to bring that into html, so if you could  
do that it would be great.

Thanks again,  
Petr

Max M. wrote:

> On 10/17/06, Petr [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Im kinda clueless about this. Any ideas?  
> > Thanks
> > 
> > Petr
> 
> Either the rails only way:
> 
> some\_array.in\_groups\_of(2)
> 
> which pads with nil, or the more ruby way:
> 
> require ‘enumerator’
> 
> some\_array.slice(2)
> 
> Cheers,  
> Max

---

<div class="post-metadata">

**Author:** ![Petr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ac8455/32.png) [@Petr](https://www.ruby-forum.com/u/Petr)\
**Post date:** [October 17, 2006, 10:43am UTC](https://www.ruby-forum.com/t/how-to-do-this/72438/3 "2006-10-17T10:43:43Z")

</div>

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

> Im kinda clueless about this. Any ideas?  
> Thanks
> 
> Petr

Either the rails only way:

some\_array.in\_groups\_of(2)

which pads with nil, or the more ruby way:

require ‘enumerator’

some\_array.slice(2)

Cheers,  
Max

---

<div class="post-metadata">

**Author:** ![Petr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ac8455/32.png) [@Petr](https://www.ruby-forum.com/u/Petr)\
**Post date:** [October 17, 2006, 4:32pm UTC](https://www.ruby-forum.com/t/how-to-do-this/72438/4 "2006-10-17T16:32:46Z")

</div>

I asked this yesterday in a post titled “Going through an array in  
batches…”

Hope this helps:

#in\_groups\_of

> **[New in Rails: Enumerable#group\_by and Array#in\_groups\_of](https://rubyonrails.org/2006/3/1/new-in-rails-enumerable-group_by-and-array-in_groups_of)**
>
> Changeset 3726 adds two little methods to ActiveSupport: Enumerable#group\_by and @ Array#in\_groups\_of@.Enumerable#group\_by is for collecting an enumerable into sets, grouped by the result of a block. Useful, for example, for grouping records by date....

\<% @products.in\_groups\_of(4) do |row| %\>

 \<% row.each do |product| %\> \<%= product.name %\> \<% end %\> \<% end -%\>

---

<div class="post-metadata">

**Author:** ![Petr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ac8455/32.png) [@Petr](https://www.ruby-forum.com/u/Petr)\
**Post date:** [October 19, 2006, 2:41am UTC](https://www.ruby-forum.com/t/how-to-do-this/72438/5 "2006-10-19T02:41:57Z")

</div>

On 10/18/06, Petr [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks.
> 
> But, you forgot to mention how to bring that into html, so if you could  
> do that it would be great.
> 
> Thanks again,  
> Petr

Use in place of some\_list.each. Instead of

\<% for thing in list %\>  
… render stuff …  
\<% end %\>

use

\<% list.in\_groups\_of(2) do |two\_things| %\>

 \<%= two\_things[0].some\_attribute if two\_things[0] %\> \<%= two\_things[1].some\_attribute if two\_things[1] %\> \<% end %\>

The nil check is necessary, as in\_groups\_of pads the last “row” with  
nils if there are not enough elements in the list.

BTW, googling for “rails in\_groups\_of” would have gotten you there as  
well…

Cheers,  
Max
