# .each do iteration question

**URL:** <https://www.ruby-forum.com/t/each-do-iteration-question/187316>\
**Category:** Rails\
**Created:** [April 21, 2010, 4:27pm UTC](https://www.ruby-forum.com/t/each-do-iteration-question/187316 "2010-04-21T16:27:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![catalin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7ba0ec/32.png) [@catalin](https://www.ruby-forum.com/u/catalin)\
**Post date:** [April 21, 2010, 4:27pm UTC](https://www.ruby-forum.com/t/each-do-iteration-question/187316/1 "2010-04-21T16:27:49Z")

</div>

I got a simple iteration going on

\<% @user.sender.each do |sender| %\>  
…  
\<% end %\>

Can I fetch and display for example only 2 items from my database insted  
of all 4 of them ?

Is there a better way to do this ?  
I could use will\_paginate on the iteration (per\_page =\> 2) but I don`t  
want the links to show up.

Cata

---

<div class="post-metadata">

**Author:** ![catalin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7ba0ec/32.png) [@catalin](https://www.ruby-forum.com/u/catalin)\
**Post date:** [April 21, 2010, 5:00pm UTC](https://www.ruby-forum.com/t/each-do-iteration-question/187316/2 "2010-04-21T17:00:58Z")

</div>

limiting the Find method in the controller.

eg:

@user = User.find(:all, :limit =\> 2)

Hope it helps!

---

<div class="post-metadata">

**Author:** ![catalin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7ba0ec/32.png) [@catalin](https://www.ruby-forum.com/u/catalin)\
**Post date:** [April 21, 2010, 6:43pm UTC](https://www.ruby-forum.com/t/each-do-iteration-question/187316/3 "2010-04-21T18:43:40Z")

</div>

Hi

> I got a simple iteration going on
> 
> \<% @user.sender.each do |sender| %\>  
> …  
> \<% end %\>
> 
> Can I fetch and display for example only 2 items from my database insted  
> of all 4 of them ?

in controller @sender = @user.sender.find(:all,:limit =\> 2)

in view  
\<% @sender.each do |s| %\>

Sijo

---

<div class="post-metadata">

**Author:** ![catalin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7ba0ec/32.png) [@catalin](https://www.ruby-forum.com/u/catalin)\
**Post date:** [April 21, 2010, 7:12pm UTC](https://www.ruby-forum.com/t/each-do-iteration-question/187316/4 "2010-04-21T19:12:11Z")

</div>

Babos C.lin wrote:

> I got a simple iteration going on
> 
> \<% @user.sender.each do |sender| %\>  
> …  
> \<% end %\>
> 
> Can I fetch and display for example only 2 items from my database insted  
> of all 4 of them ?
> 
> Is there a better way to do this ?  
> I could use will\_paginate on the iteration (per\_page =\> 2) but I don`t  
> want the links to show up.
> 
> Cata

Which two are you interested in seeing?

The first two, or two with certain attributes. Is this a space issue or  
a selection issue?

You could create a named scope I think, then refer to

@user.special\_senders.each do |sender|

where the special\_senders scope limits the associated senders to only  
those you want.

---

<div class="post-metadata">

**Author:** ![catalin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7ba0ec/32.png) [@catalin](https://www.ruby-forum.com/u/catalin)\
**Post date:** [April 22, 2010, 12:18am UTC](https://www.ruby-forum.com/t/each-do-iteration-question/187316/5 "2010-04-22T00:18:17Z")

</div>

Sijo k g wrote:

> Hi
> 
> > I got a simple iteration going on
> > 
> > \<% @user.sender.each do |sender| %\>  
> > …  
> > \<% end %\>
> > 
> > Can I fetch and display for example only 2 items from my database insted  
> > of all 4 of them ?
> 
> in controller @sender = @user.sender.find(:all,:limit =\> 2)
> 
> in view  
> \<% @sender.each do |s| %\>
> 
> Sijo

Thank you very much, that did the trick.  
It was a selection issue and I think I will use a named scope later on.

Cata
