On 25 March 2011 20:33, John Merlino <li...@ruby-forum.com> wrote: > Hey all, > > I'm trying to fully understand what this method in Ruby does. Basically > it passes a collection of records from the database (using the Rails > find method) into the argument list as a local variable called > resources. It assigns the local variable to an instance variable called > @all_resources. Then it checks the query string in the url to see if the > limit key has a value and if so grab it(e.g. ?limit=10), otherwise > default to 25. Then it checks the query string for a page key and if > none is there, it defaults to 1 and assigns it to instance variable > @page. Then we subtract 1 from @page, presumably because if we are on > page 1, then 1 -1 = 0, so 0 is assigned to @page_index. This means when > we multiply page_index by 25 (limit), we get 0, and so when we slice the > resources we slice the limit (25) by offset (0), and hence we return the > first 25 records, making sense since we want to display first 35 records > on page 1. However, if $page is equal to 2 and limit 25, then > page_index is equal to 1, and offset equal to 25. But I dont see how we > are getting the next 25 records on page 2.
Have a look at the Rails Guide on debugging and find out how to use ruby-debug to break into the code and inspect data. Then break in at the appropriate points and see what is happening. By a combination of examining variables, entering expressions for evaluation to see what the results are and stepping through the code you should see how (if) it works. > > I think what's confusing me is this line of code: > > ((@all_resources.size - 1) / @limit).to_i + 1 > > What does the size method do and why add one at the end? The size method is Array#size and returns the number of elements in the array, ie the number of records in @all_resources in this case. Colin > > Here's the method: > > def paginate(resources) > @all_resources = resources > @limit = (params[:limit] && params[:limit].to_i) || 25 > @page = (params[:page] && params[:page].to_i) || 1 > @page_index = @page - 1 > @offset = @page_index * @limit > @num_pages = if @limit == 0 > 1 > elsif @all_resources.size == 0 > 1 > else > ((@all_resources.size - 1) / @limit).to_i + 1 > end > @last_page = @num_pages > @limit == 0 ? @all_resources : @all_resources.slice(@offset, @limit) > end > > Thanks for response > > -- > 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@googlegroups.com. > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > > -- 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@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.