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

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

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
[email protected]

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

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

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!

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. [email protected]

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

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

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.” [email protected]