# 3 columns

**URL:** <https://www.ruby-forum.com/t/3-columns/99103>\
**Category:** Rails\
**Created:** [July 20, 2006, 9:14am UTC](https://www.ruby-forum.com/t/3-columns/99103 "2006-07-20T09:14:05Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![jgeggatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jgeggatt](https://www.ruby-forum.com/u/jgeggatt)\
**Post date:** [July 20, 2006, 9:14am UTC](https://www.ruby-forum.com/t/3-columns/99103/1 "2006-07-20T09:14:05Z")

</div>

Hi all,

Not really sure what to put in the title so hope people still open this!

What I want to do is produce something like this

Col1 Col2 Col3  
data1 data2 data3  
data4 data5 data6  
data7 data8 data9  
data… data… data…  
data… data… data…  
data-n data-n+1 data-n+2

I can get the data back from my db happily, but I cant for the life of  
me work out how to actually display it. Single column is easy, but  
anything else has me stumped.

Can anyone help?

Thanks

Jonathan

---

<div class="post-metadata">

**Author:** ![jgeggatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jgeggatt](https://www.ruby-forum.com/u/jgeggatt)\
**Post date:** [July 20, 2006, 10:07am UTC](https://www.ruby-forum.com/t/3-columns/99103/2 "2006-07-20T10:07:42Z")

</div>

I’d normally have an iterator that increments each loop and if it’s  
divisible by 3 print a closing tr tag and start another table row.  
other than that print every value inside a table cell.

Ross

---

<div class="post-metadata">

**Author:** ![jgeggatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jgeggatt](https://www.ruby-forum.com/u/jgeggatt)\
**Post date:** [July 20, 2006, 10:17am UTC](https://www.ruby-forum.com/t/3-columns/99103/3 "2006-07-20T10:17:48Z")

</div>

Ross R. wrote:

> I’d normally have an iterator that increments each loop and if it’s  
> divisible by 3 print a closing tr tag and start another table row.  
> other than that print every value inside a table cell.
> 
> Ross

if @results contains ur class objects:

\<% @results.each do |obj| -%\> \<% end -%\>

| \<%= obj.col1 %\> | \<%= obj.col2 %\> | \<%= obj.col3 %\> |

---

<div class="post-metadata">

**Author:** ![jgeggatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jgeggatt](https://www.ruby-forum.com/u/jgeggatt)\
**Post date:** [July 20, 2006, 10:26am UTC](https://www.ruby-forum.com/t/3-columns/99103/4 "2006-07-20T10:26:43Z")

</div>

Yes but that only works if you are doing one table row per db row. I  
thought the original question implied that he was iterating through a  
DB and every third DB row would drop to another table row.

Ross

---

<div class="post-metadata">

**Author:** ![jgeggatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jgeggatt](https://www.ruby-forum.com/u/jgeggatt)\
**Post date:** [July 20, 2006, 10:39am UTC](https://www.ruby-forum.com/t/3-columns/99103/5 "2006-07-20T10:39:14Z")

</div>

Ross R. wrote:

> Yes but that only works if you are doing one table row per db row. I  
> thought the original question implied that he was iterating through a  
> DB and every third DB row would drop to another table row.
> 
> Ross

Something like that?

\<% @results.each\_with\_index do |obj, i| -%\> \<%= "" if 2 == i.modulo(3) -%\> \<% end -%\>

| \<%= obj.col1 %\> | \<%= obj.col2 %\> | \<%= obj.col3 %\> |
|
| |

---

<div class="post-metadata">

**Author:** ![jgeggatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jgeggatt](https://www.ruby-forum.com/u/jgeggatt)\
**Post date:** [July 20, 2006, 10:45am UTC](https://www.ruby-forum.com/t/3-columns/99103/6 "2006-07-20T10:45:56Z")

</div>

Yes that’s what I was thinking…

---

<div class="post-metadata">

**Author:** ![jgeggatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jgeggatt](https://www.ruby-forum.com/u/jgeggatt)\
**Post date:** [July 20, 2006, 11:24am UTC](https://www.ruby-forum.com/t/3-columns/99103/7 "2006-07-20T11:24:01Z")

</div>

Thanks to both of you! Saved me hours of head scratching!

What I ended up with is the following,

\<% @results.each\_with\_index do |obj, i| -%\> \<%= "" if 2 == i.modulo(3) -%\> \<% end -%\>

| \<%= obj.columnvalue%\> |
|
| |

Which gives me the following:

record1.colvalue record2.colvalue record3.colvalue  
record4.colvalue record5.colvalue record6.colvalue  
record7.colvalue record8.colvalue record9.colvalue

Thanks once again for pushing me in the right direction.

Jonathan

---

<div class="post-metadata">

**Author:** ![jgeggatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jgeggatt](https://www.ruby-forum.com/u/jgeggatt)\
**Post date:** [July 20, 2006, 11:51am UTC](https://www.ruby-forum.com/t/3-columns/99103/8 "2006-07-20T11:51:10Z")

</div>

Did you try in\_groups\_of ?

Check out  
[http://weblog.rubyonrails.com/2006/3/1/new-in-rails-enumerable-group\_by-and-array-in\_groups\_of](http://weblog.rubyonrails.com/2006/3/1/new-in-rails-enumerable-group_by-and-array-in_groups_of)  
for a short tutorial.

---

<div class="post-metadata">

**Author:** ![jgeggatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jgeggatt](https://www.ruby-forum.com/u/jgeggatt)\
**Post date:** [July 20, 2006, 12:05pm UTC](https://www.ruby-forum.com/t/3-columns/99103/9 "2006-07-20T12:05:58Z")

</div>

oomtuinstoel wrote:

> Did you try in\_groups\_of ?
> 
> Check out  
> [Peak Obsession](http://weblog.rubyonrails.com/2006/3/1/new-in-rails-enumerable-group_by-and-array-in_groups_of)  
> for a short tutorial.

Very nice!

---

<div class="post-metadata">

**Author:** ![jgeggatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jgeggatt](https://www.ruby-forum.com/u/jgeggatt)\
**Post date:** [July 20, 2006, 12:08pm UTC](https://www.ruby-forum.com/t/3-columns/99103/10 "2006-07-20T12:08:53Z")

</div>

Hello Jonathan,

> Thanks to both of you! Saved me hours of head scratching!
> 
> What I ended up with is the following,
> 
> \<% @results.each\_with\_index do |obj, i| -%\> \<%= "" if 2 == i.modulo(3) -%\> \<% end -%\>
> 
> | \<%= obj.columnvalue%\> |
> |
> | |

You can also use Array#in\_groups\_of :

\<% @results.map(&:columnvalue).in\_groups\_of(3, fill\_with='&nbsp;') do |group| -%\> \<% for col in group -%\> \<% end -%\> \<% end -%\>

| \<%= col %\> |

There will be "

&nbsp;" code that will pad your table  
if the size of @results is not a multiple of 3.

– Jean-FranÃ§ois.

---

<div class="post-metadata">

**Author:** ![jgeggatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jgeggatt](https://www.ruby-forum.com/u/jgeggatt)\
**Post date:** [October 22, 2006, 9:20pm UTC](https://www.ruby-forum.com/t/3-columns/99103/11 "2006-10-22T21:20:16Z")

</div>

Ok so I’m a newbie, and I’m confused!

I want to also disply the reulst in columns, nut only two. I begin my  
loop with:

\<% for @post in @posts %\>

\<% end %\>

How can I get this to display in two colums rather than one?  
Thanks

---

<div class="post-metadata">

**Author:** ![jgeggatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jgeggatt](https://www.ruby-forum.com/u/jgeggatt)\
**Post date:** [October 24, 2006, 7:09pm UTC](https://www.ruby-forum.com/t/3-columns/99103/12 "2006-10-24T19:09:01Z")

</div>

Here is what i got so far.

\<% (@post = Post.find(:all)).to\_a.in\_groups\_of(3) do |group| %\> \<% group.each do |title| %\> \<% end %\> \<% end %\>

| \<%= title %\> |

title = column value in db.

I get in return the right table div’s, but no info only hashes. It looks  
like this when render:

### 

# 

So I know the columns are working, but how do I get it to display the  
data?
