# Different each iteration

**URL:** <https://www.ruby-forum.com/t/different-each-iteration/190122>\
**Category:** Rails\
**Created:** [June 16, 2010, 10:19am UTC](https://www.ruby-forum.com/t/different-each-iteration/190122 "2010-06-16T10:19:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [June 16, 2010, 10:19am UTC](https://www.ruby-forum.com/t/different-each-iteration/190122/1 "2010-06-16T10:19:20Z")

</div>

Hi,  
I’ve read a few days ago that rails has a each method that allows you to  
speficy the number of iteration. I think the default number was 1000.  
What is the name of the method?

myList.each\_with\_something or myList.each\_something

Greg

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [June 16, 2010, 10:30am UTC](https://www.ruby-forum.com/t/different-each-iteration/190122/2 "2010-06-16T10:30:59Z")

</div>

Greg Ma wrote:

> Hi,  
> I’ve read a few days ago that rails has a each method that allows you to  
> speficy the number of iteration. I think the default number was 1000.  
> What is the name of the method?
> 
> myList.each\_with\_something or myList.each\_something
> 
> Greg

You can specify the batch\_size as an option to a find\_each method. Batch  
methods should be used when your request retrieves large amounts of  
data:

“When processing large numbers of records, it’s often a good idea to do  
so in batches to prevent memory ballooning.”

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [June 16, 2010, 3:07pm UTC](https://www.ruby-forum.com/t/different-each-iteration/190122/3 "2010-06-16T15:07:36Z")

</div>

Pale H. wrote:

> Greg Ma wrote:
> 
> > Hi,  
> > I’ve read a few days ago that rails has a each method that allows you to  
> > speficy the number of iteration. I think the default number was 1000.  
> > What is the name of the method?
> > 
> > myList.each\_with\_something or myList.each\_something
> > 
> > Greg
> 
> You can specify the batch\_size as an option to a find\_each method. Batch  
> methods should be used when your request retrieves large amounts of  
> data:
> 
> “When processing large numbers of records, it’s often a good idea to do  
> so in batches to prevent memory ballooning.”

There’s also in\_groups\_of.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [June 16, 2010, 3:37pm UTC](https://www.ruby-forum.com/t/different-each-iteration/190122/4 "2010-06-16T15:37:34Z")

</div>

Marnen Laibow-Koser wrote:

> There’s also in\_groups\_of.
> 
> ## Best,
> 
> Marnen Laibow-Koser  
> [http://www.marnen.org](http://www.marnen.org)  
> removed\_email\_address@domain.invalid

I’d forgotten about that method. In addition, you can  
each\_slice(integer) to divide an array. Though, ‘enumerator’ is required  
in your Active Record model. I would recommend this approach if you’re  
splitting arrays into columns or rows. Else, batch\_size and in\_groups\_of  
are useful for retrieving large data sets.

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [June 17, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/different-each-iteration/190122/5 "2010-06-17T16:22:19Z")

</div>

On 16 June 2010 09:19, Greg Ma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> myList.each\_with\_something or myList.each\_something

Do you mean the “each\_slice” method of enumerable?

[http://ruby-doc.org/core/classes/Enumerable.html](http://ruby-doc.org/core/classes/Enumerable.html)

---

<div class="post-metadata">

**Author:** ![gregm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc79bd/32.png) [@gregm](https://www.ruby-forum.com/u/gregm)\
**Post date:** [June 17, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/different-each-iteration/190122/6 "2010-06-17T16:22:16Z")

</div>

does u speak about it ?  
[http://api.rubyonrails.org/classes/ActiveRecord/Batches/ClassMethods.html](http://api.rubyonrails.org/classes/ActiveRecord/Batches/ClassMethods.html)

Ivan N.  
removed\_email\_address@domain.invalid
