# I want to Use each block only a certain # of times

**URL:** <https://www.ruby-forum.com/t/i-want-to-use-each-block-only-a-certain-of-times/142598>\
**Category:** Rails\
**Created:** [July 9, 2008, 4:22pm UTC](https://www.ruby-forum.com/t/i-want-to-use-each-block-only-a-certain-of-times/142598 "2008-07-09T16:22:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![THEBIGO](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea666f/32.png) [@THEBIGO](https://www.ruby-forum.com/u/THEBIGO)\
**Post date:** [July 9, 2008, 4:22pm UTC](https://www.ruby-forum.com/t/i-want-to-use-each-block-only-a-certain-of-times/142598/1 "2008-07-09T16:22:37Z")

</div>

I’m a newb with a simple question I have been researching everywhere

I have a block \<% @current\_user.calls.each do |c| %  
\<%= link\_to c.name, etc,blah blah blah -%\>  
\<& end %\>

and it does this for all the current\_user\_calls I have but I want to  
only show the last 5  
Thery are laready in the order I want so is there a way to tell the  
each to only do it 5 times

Any help will be appreciated

---

<div class="post-metadata">

**Author:** ![THEBIGO](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea666f/32.png) [@THEBIGO](https://www.ruby-forum.com/u/THEBIGO)\
**Post date:** [July 9, 2008, 4:52pm UTC](https://www.ruby-forum.com/t/i-want-to-use-each-block-only-a-certain-of-times/142598/2 "2008-07-09T16:52:08Z")

</div>

On Jul 9, 2008, at 10:09 AM, THEBIGO wrote:

> each to only do it 5 times
> 
> Any help will be appreciated

@current\_user.calls.first(5).each do |c|  
or  
@current\_user.calls.last(5).each do |c|

depending on which order they are already in compared to what you want  
to see.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![THEBIGO](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea666f/32.png) [@THEBIGO](https://www.ruby-forum.com/u/THEBIGO)\
**Post date:** [July 9, 2008, 4:57pm UTC](https://www.ruby-forum.com/t/i-want-to-use-each-block-only-a-certain-of-times/142598/3 "2008-07-09T16:57:16Z")

</div>

On 9 Jul 2008, at 15:09, THEBIGO wrote:

> each to only do it 5 times  
> You can’t. You can however give each an array with the correct number  
> of elements, eg  
> @current\_user.calls[0,5].each …

If .calls is an activerecord associations then that’s not a great idea  
because you’re loading all of them, only to discard all but the first 5.

current\_user.calls.find :all, :limit =\> 5

will only load 5 calls etc…

Fred

---

<div class="post-metadata">

**Author:** ![THEBIGO](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea666f/32.png) [@THEBIGO](https://www.ruby-forum.com/u/THEBIGO)\
**Post date:** [July 9, 2008, 5:15pm UTC](https://www.ruby-forum.com/t/i-want-to-use-each-block-only-a-certain-of-times/142598/4 "2008-07-09T17:15:14Z")

</div>

Frederick C. wrote:

> On 9 Jul 2008, at 15:09, THEBIGO wrote:
> 
> If .calls is an activerecord associations then that’s not a great idea  
> because you’re loading all of them, only to discard all but the first 5.
> 
> current\_user.calls.find :all, :limit =\> 5
> 
> will only load 5 calls etc…
> 
> Fred

except you can’t eager load associations with a limit…  
so if it weren’t just for current user,  
but, instead;

@all\_users.each do |user|  
user.calls.last(5).each do |call|  
…  
end  
end

or

@all\_users.each do |user|  
user.calls.find(:all, :limit =\> 5, :order =\> “id DESC”).each do |call|  
…  
end  
end

I’d go for the former  
I guess it’s a toss up between eager-loading too much, and doing too  
many smaller queries…

Matthew R.  
[http://www.workingwithrails.com/person/12394-matthew-rudy-jacobs](http://www.workingwithrails.com/person/12394-matthew-rudy-jacobs)

---

<div class="post-metadata">

**Author:** ![THEBIGO](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea666f/32.png) [@THEBIGO](https://www.ruby-forum.com/u/THEBIGO)\
**Post date:** [July 9, 2008, 5:47pm UTC](https://www.ruby-forum.com/t/i-want-to-use-each-block-only-a-certain-of-times/142598/5 "2008-07-09T17:47:16Z")

</div>

THEBIGO wrote:

> Thanks much Fredrick but I’m not sure what to do with the code
> 
> So what gets shown in the view is a link to each call by name but it  
> lists all the calls and I only want 5 to show  
> The links work and go to the show action for the calls fine. So I am  
> not sure how to limit the number of calls shown  
> Any hlp will be appreciated - maybe I need a different loop. The  
> calls all have call.id’s not in order because different users  
> createcalls but  
> I handle the associations fine

well do exactly what Frhodri suggested;

\<% recent\_calls = current\_user.calls.find(:all, :limit =\> 5, :order =\>  
“id DESC”) %\>  
\<% if recent\_calls.blank? %\>

### You have no calls

- \<%= link\_to 'Add a call to get started!', { :controller =\> "calls", :action =\> "new" } %\>

\<% else %\> 
### My Recent Calls

 \<% recent\_calls.each do |call| %\> - \<%= link\_to call.name, {:controller =\> "calls", :action =\> 'show', :id =\> call.id} -%\>
 \<% end %\> 
\<% end %\>

EASY!

---

<div class="post-metadata">

**Author:** ![THEBIGO](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea666f/32.png) [@THEBIGO](https://www.ruby-forum.com/u/THEBIGO)\
**Post date:** [July 9, 2008, 5:24pm UTC](https://www.ruby-forum.com/t/i-want-to-use-each-block-only-a-certain-of-times/142598/6 "2008-07-09T17:24:55Z")

</div>

Thanks much Fredrick but I’m not sure what to do with the code  
What I have is the followint

\<% if @current\_user.calls.blank? %\>

### You have no calls
  

  
- \<%= link\_to ‘Add a call to get started!’, { :controller =\>  
“calls”, :action =\> “new” } %\>
  

  
\<% else %\>  

### My Recent Calls
  

  
\<% @current\_user.calls.each do |c| %\>  
- \<%= link\_to c.name, {:controller =\> “calls”, :action =\>  
‘show’, :id =\> c.id} -%\>
  
\<% end %\>  

  
\<% end %\>

So what gets shown in the view is a link to each call by name but it  
lists all the calls and I only want 5 to show  
The links work and go to the show action for the calls fine. So I am  
not sure how to limit the number of calls shown  
Any hlp will be appreciated - maybe I need a different loop. The  
calls all have call.id’s not in order because different users  
createcalls but  
I handle the associations fine

On Jul 9, 7:14 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![THEBIGO](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea666f/32.png) [@THEBIGO](https://www.ruby-forum.com/u/THEBIGO)\
**Post date:** [July 9, 2008, 7:12pm UTC](https://www.ruby-forum.com/t/i-want-to-use-each-block-only-a-certain-of-times/142598/7 "2008-07-09T19:12:11Z")

</div>

Please don’t use finds, let alone finds with SQL, in views. Hide all  
that  
behind a well-named method on your Call model, create an extension named  
“recent” on your has\_many :calls association on User or use named\_scope  
(in  
Rails 2.1.x), and let your controller set up the data for the view.  
Since it  
looks like you might be using Rails 1.2.x, try something like this.

class Call \< ActiveRecord::Base  
…  
def self.find\_recent  
find(:all, :limit =\> 5, :order =\> “id DESC”)  
end  
…  
end

class User \< ActiveRecord::Base  
…  
has\_many :calls  
…  
end

class WhateverController \< …  
…  
def whatever\_action\_is\_being\_used\_in\_your\_example  
@recent\_calls = current\_user.calls.find\_recent  
end  
…  
end

# in your view

\<%- if @recent\_calls.blank? -%\>

### You have no calls
... \<%- else -%\>
### My Recent Calls

 \<%- @recent\_calls.each do |c| -%\>- \<%= link\_to c.name, {:controller =\> "calls", :action =\> 'show', :id =\> c.id} %\>
\<%- end -%\>
\<%- end -%\>

An approach like this keeps everything in its place in the MVC structure  
that Rails provides. If you’d like to explore using the association  
extension or named\_scope, let the list know.

Regards,  
Craig

---

<div class="post-metadata">

**Author:** ![THEBIGO](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea666f/32.png) [@THEBIGO](https://www.ruby-forum.com/u/THEBIGO)\
**Post date:** [July 9, 2008, 5:50pm UTC](https://www.ruby-forum.com/t/i-want-to-use-each-block-only-a-certain-of-times/142598/8 "2008-07-09T17:50:13Z")

</div>

Rob I think this works and will try some more much thanks for all your  
help and for everyones help  
This is a great group

---

<div class="post-metadata">

**Author:** ![THEBIGO](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea666f/32.png) [@THEBIGO](https://www.ruby-forum.com/u/THEBIGO)\
**Post date:** [July 9, 2008, 8:15pm UTC](https://www.ruby-forum.com/t/i-want-to-use-each-block-only-a-certain-of-times/142598/9 "2008-07-09T20:15:34Z")

</div>

Thanks again all and thanks Craig

Actually I do all my finds in the user controller. I had also done  
something with the user.rb file which I now see should have been in  
the  
calls.rb file so I will try that again. right now the suggestion by  
Rob is working great so I appreciate all the lessons and input

Owen

On Jul 9, 10:11 am, “Craig D.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
