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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Jul-09 14:14 UTC
Re: I want to Use each block only a certain # of times
On 9 Jul 2008, at 15:09, THEBIGO wrote:> > 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 timesYou 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn
2008-Jul-09 14:48 UTC
Re: I want to Use each block only a certain # of times
On Jul 9, 2008, at 10:09 AM, THEBIGO wrote:> > 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@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 Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Matthew Rudy Jacobs
2008-Jul-09 15:15 UTC
Re: I want to Use each block only a certain # of times
Frederick Cheung 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... > > Fredexcept 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 Rudy http://www.workingwithrails.com/person/12394-matthew-rudy-jacobs -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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? %> <h3>You have no calls</h3> <ul> <li><%= link_to ''Add a call to get started!'', { :controller => "calls", :action => "new" } %></li> </ul> <% else %> <h3>My Recent Calls</h3> <ul> <% @current_user.calls.each do |c| %> <li><%= link_to c.name, {:controller => "calls", :action => ''show'', :id => c.id} -%></li> <% end %> </ul> <% 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 Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 9 Jul 2008, at 15:09, THEBIGO wrote: > > > > > 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 > > 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Matthew Rudy Jacobs
2008-Jul-09 15:47 UTC
Re: I want to Use each block only a certain # of times
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 finewell do exactly what Frhodri suggested; <% recent_calls = current_user.calls.find(:all, :limit => 5, :order => "id DESC") %> <% if recent_calls.blank? %> <h3>You have no calls</h3> <ul> <li><%= link_to ''Add a call to get started!'', { :controller => "calls", :action => "new" } %></li> </ul> <% else %> <h3>My Recent Calls</h3> <ul> <% recent_calls.each do |call| %> <li><%= link_to call.name, {:controller => "calls", :action => ''show'', :id => call.id} -%></li> <% end %> </ul> <% end %> EASY! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On Jul 9, 8:23 am, THEBIGO <Mowenh...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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? %> > <h3>You have no calls</h3> > <ul> > <li><%= link_to ''Add a call to get started!'', { :controller => > "calls", :action => "new" } %></li> > </ul> > <% else %> > <h3>My Recent Calls</h3> > <ul> > <% @current_user.calls.each do |c| %> > <li><%= link_to c.name, {:controller => "calls", :action => > ''show'', :id => c.id} -%></li> > <% end %> > </ul> > <% 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 Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > On 9 Jul 2008, at 15:09, THEBIGO wrote: > > > > 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 > > > 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Craig Demyanovich
2008-Jul-09 17:11 UTC
Re: I want to Use each block only a certain # of times
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? -%> <h3>You have no calls</h3> ... <%- else -%> <h3>My Recent Calls</h3> <ul> <%- @recent_calls.each do |c| -%> <li><%= link_to c.name, {:controller => "calls", :action => ''show'', :id => c.id} %></li> <%- end -%> </ul> <%- 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Demyanovich" <cdemyanov...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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? -%> > <h3>You have no calls</h3> > ... > <%- else -%> > <h3>My Recent Calls</h3> > <ul> > <%- @recent_calls.each do |c| -%> > <li><%= link_to c.name, {:controller => "calls", :action => ''show'', :id => > c.id} %></li> > <%- end -%> > </ul> > <%- 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---