# Working with in\_groups\_of

**URL:** <https://www.ruby-forum.com/t/working-with-in-groups-of/162339>\
**Category:** Rails\
**Created:** [March 8, 2009, 10:57pm UTC](https://www.ruby-forum.com/t/working-with-in-groups-of/162339 "2009-03-08T22:57:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rsanheim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/47e85d/32.png) [@rsanheim](https://www.ruby-forum.com/u/rsanheim)\
**Post date:** [March 8, 2009, 10:57pm UTC](https://www.ruby-forum.com/t/working-with-in-groups-of/162339/1 "2009-03-08T22:57:40Z")

</div>

I’m trying to build a table out using in groups of, but I’d also like to  
be able to customize the first and last rows.

Apart from the actual formatting, is there an efficient way to figure  
out how many total groups there are and what group the iteration is in?

---

<div class="post-metadata">

**Author:** ![rsanheim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/47e85d/32.png) [@rsanheim](https://www.ruby-forum.com/u/rsanheim)\
**Post date:** [March 9, 2009, 6:28pm UTC](https://www.ruby-forum.com/t/working-with-in-groups-of/162339/2 "2009-03-09T18:28:08Z")

</div>

On Sun, Mar 8, 2009 at 4:57 PM, Rob Scott  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m trying to build a table out using in groups of, but I’d also like to  
> be able to customize the first and last rows.
> 
> Apart from the actual formatting, is there an efficient way to figure  
> out how many total groups there are and what group the iteration is in?

users = User.find(:all).in\_groups\_of( 3 )

users.first and users.last will be the first and last groups  
respectively.

users.size will be the total groups of users.

And then going forward you can figure all this out on your own using  
users.methods.sort… like seeing that users.class is an Array will let  
you know you can use anything from the Array class on your users  
collection.

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)
