#240 ✓invalid
Maggie

will_paginate repeat problem

Reported by Maggie | August 9th, 2008 @ 11:42 PM

I am fairly new to RoR, and completely new to will_paginate. I have everything working except one page that is unhappy. In the view I get duplicates of page 1, and per_page is ignored. I suspect it is because I am working with an :include.

This is my controller: def project_list

   @cats = Cat.find(params[:id], :include => :projects)
   @projects = Project.paginate :page => params[:pages],   per_page => 4,  :order =>'date DESC'

end

Any suggestions would be greatly appreciated.

Comments and changes to this ticket

  • Maggie

    Maggie August 10th, 2008 @ 03:49 AM

    I'm sorry if this is the wrong place to be asking very beginner questions. I will stop if I this is inappropriate.

    I fixed my controller so that :order is correct. I added to my model but have not solved the issue of many (6) repeating pages.

    Controller: def project_list

       @cats = Cat.find(params[:id],:order =>'date DESC', :include => :projects)
       @projects = Project.paginate :page => params[:pages], :per_page => 4
      end
    

    Model: cattr_reader :per_page

    @@per_page = 4
    
    def self.display_project_data(page)
    paginate(:page => page, :per_page => 4)
    
    end
    

    I think that the def self. ... was about working with a plugin.

    thanks.

  • Mislav

    Mislav August 10th, 2008 @ 01:41 PM

    Hey Maggie,

    Unless you think this is a will_paginate bug, support questions are better asked on the "will_paginate" Google group. But now that you asked here, let's see what this is.

    First of all I don't understand your problems or some your code. I don't see what is the purpose of @cats -- it seems that it has nothing to do with pagination of projects, so it can't affect it.

    Also, I see you specify an explicit ":per_page => 4" when paginating -- that's OK, but then you don't have to do this:

    
    cattr_reader :per_page
    @@per_page = 4
    

    But first you have to explain exactly what you mean by "pages are repeating".

  • Maggie

    Maggie August 10th, 2008 @ 02:05 PM

    Hi Mislav, Thanks so much for getting back to me. I realized that I was in the wrong place. I promise I won't do that again. If only I was at the level of trouble shooting a bug!

    I am using the @cats id to select from the projects table for the project_list view.

    I am developing my portfolio website. I am looking for work designing from construction up for education. I strongly believe in throwing away the idea of purchasing propriety software and developing as needed, especially for education.

    This is my website unpublished site. http://www.maggiereid.com/index3...

    Go to this page and choose which category. http://www.portfolio.maggiereid....

    This is the page with the problem. http://www.portfolio.maggiereid....

    I am so happy that everything else is working. Thank you for helping to develop this.

    Thanks so much, Maggie ps. I took this out of my model. I was trying many things. @@@ ruby

    cattr_reader :per_page @@per_page = 4

    On Aug 10, 2008, at 7:41 AM, Lighthouse wrote:

  • Mislav

    Mislav August 10th, 2008 @ 03:26 PM

    • State changed from “new” to “invalid”

    Ah, I see now where your troubles are.

    Thanks for showing me, I didn't understand that "cats" was short for "categories". In Ruby, it's best to avoid abbreviations and use full names for better code readability. So, in your place, I would rename the "Cat" model to "Category".

    Your first problem is here:

    
    :page => params[:pages]
    

    You have a typo. You used ":pages" instead of ":page". In the last link you've pasted, observe that the GET parameter name is singular, not plural.

    Next, you want to display only graphic design projects under the "Graphic Design" category, right? Well, you're doing it wrong. First you are fetching the category, that's kinda OK, but in the next line your are paginating all projects regardless of category. I don't think that's what you wanted.

    This is correct:

    
    def project_list
      @category = Cat.find(params[:id])
      @projects = @category.projects.paginate :page => params[:page], :per_page => 4
    end
    

    You can see that the first line is significantly shorter, and that in the second line I'm scoping projects for pagination to ones belonging to current category.

    So what was wrong with this line?

    
    Cat.find(params[:id], :order =>'date DESC', :include => :projects)
    

    You're passing an ID for the first parameter, that means you are fetching a single category, not a collection. When passing an ID, the :order parameter has absolutely no effect, so you can remove it (as I did). Also, eager loading with :include also doesn't make sense when fetching a single object by its ID. You want to paginate projects from this category, so it's better not to eager load them, but to let pagination handle loading in the second line:

    
    @category.projects.paginate ...
    
  • Maggie

    Maggie August 10th, 2008 @ 04:25 PM

    Followed your instructions and am happy to take out the include. I didn't rename the model, but I will not abbreviate in my next fun project which I am now mapping out. At this point I'm going for as few typos as possible to simplify trouble shooting!

    Made progress, but not there yet. :order and :per_page not working. Have moved from having 5 duplicate paginated pages to 2.

    def project_list

     @cats = Cat.find(params[:id])
     @projects = @cats.projects.paginate :page => params
    

    [:page], :per_page => 4, :order =>'date DESC' end

    So what was wrong with this line?

    
    Cat.find(params[:id], :order =>'date DESC', :include => :projects)
    
    This was the only way that I was able to :order
    
    Thanks again!
    
  • Mislav

    Mislav August 10th, 2008 @ 08:17 PM

    I'm not sure. The line that you have should be working:

    
    @projects = @cats.projects.paginate :page => params[:page], :per_page => 4, :order => 'date DESC'
    

    :per_page and :order should have effect, and if they don't then something is terribly wrong.

    What version of Rails and will_paginate are you running?

    You could also do this, it would be very useful: first make sure you're using the exact code I last pasted you, then visit the project_list action in your web browser, then open log/development.log and paste me the SQL queries which were performed while rendering that action.

    As for your question:

    
    Cat.find(params[:id], :order => 'date DESC', :include => :projects)
    

    I already explained why this code is wrong in my last message. I don't know how :order worked for you; theoretically it should have absolutely no effect. The :include is wrong because it will load all the projects belonging to that category, but we want to load only the single page. If there are 100 projects, we only want to load the first 4, then the next 4 on page 2, and so on. This is what will_paginate is doing for us in the "paginate" method.

    It may have worked for you, but, because of changes to internal implementation of eager loading in newer versions of Rails, your code may stop working. That's why it's better to follow correct practices.

  • Maggie

    Maggie August 10th, 2008 @ 11:40 PM

    I am on shared server at www.railsplayground.com

    rails -v 2.1.0 will_paginate -v 2.2.2

    Your exact controller code (breaks):

      def project_list
        @category = Cat.find(params[:id])
        @projects = @category.projects.paginate :page => params
    

    [:page], :per_page > 4

       end
    

    Processing ApplicationController#project_list (for 207.237.77.240 at 2008-08-10 16:31:06) [GET] Session ID: d32eda2e277f64a1a2455b03d4836c41 Parameters: {"action"=>"project_list", "id"=>"2", "controller"=>"portfolio"}

    SyntaxError (/home/maggie/portfolio/app/controllers/ portfolio_controller.rb:81: syntax error, unexpected '\n', expecting tASSOC /home/maggie/portfolio/app/controllers/portfolio_controller.rb:176: syntax error, unexpected $end, expecting kEND end

      ^):
     /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:
    

    27:in gem_original_require'

     /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:
    

    27:in require'

     /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/
    

    active_support/dependencies.rb:495:in require'

     /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/
    

    active_support/dependencies.rb:342:in new_constants_in'

     /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/
    

    active_support/dependencies.rb:495:in require'

     /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/
    

    active_support/dependencies.rb:104:in require_or_load'

     /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/
    

    active_support/dependencies.rb:248:in load_missing_constant'

     /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/
    

    active_support/dependencies.rb:452:in const_missing'

     /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/
    

    active_support/dependencies.rb:464:in const_missing'

     /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/
    

    active_support/inflector.rb:250:in constantize'

     /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/
    

    active_support/core_ext/string/inflections.rb:148:in constantize'

     /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/
    

    action_controller/routing.rb:1317:in recognize'

     /usr/local/lib/ruby/gems/1.8/gems/rails-1.2.6/lib/dispatcher.rb:
    

    40:in dispatch'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/lib/passenger/
    

    railz/request_handler.rb:38:in process_request'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/lib/passenger/
    

    abstract_request_handler.rb:163:in main_loop'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/lib/passenger/
    

    railz/application_spawner.rb:307:in start_request_handler'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/lib/passenger/
    

    railz/application_spawner.rb:276:in handle_spawn_application'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/lib/passenger/
    

    utils.rb:165:in safe_fork'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/lib/passenger/
    

    utils.rb:163:in fork'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/lib/passenger/
    

    utils.rb:163:in safe_fork'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/lib/passenger/
    

    railz/application_spawner.rb:274:in handle_spawn_application'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/lib/passenger/
    

    utils.rb:165:in safe_fork'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/lib/passenger/
    

    utils.rb:163:in fork'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/lib/passenger/
    

    utils.rb:163:in safe_fork'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/lib/passenger/
    

    railz/application_spawner.rb:273:in handle_spawn_application'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/lib/passenger/
    

    abstract_server.rb:317:in __send__'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/lib/passenger/
    

    abstract_server.rb:317:in main_loop'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/lib/passenger/
    

    abstract_server.rb:168:in start_synchronously'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/lib/passenger/
    

    abstract_server.rb:135:in start'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/lib/passenger/
    

    abstract_server.rb:112:in fork'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/lib/passenger/
    

    abstract_server.rb:112:in start'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/lib/passenger/
    

    railz/application_spawner.rb:177:in start'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/lib/passenger/
    

    railz/framework_spawner.rb:270:in handle_spawn_application'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/lib/passenger/
    

    railz/framework_spawner.rb:263:in synchronize'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/lib/passenger/
    

    railz/framework_spawner.rb:263:in handle_spawn_application'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/lib/passenger/
    

    abstract_server.rb:317:in __send__'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/lib/passenger/
    

    abstract_server.rb:317:in main_loop'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/lib/passenger/
    

    abstract_server.rb:168:in start_synchronously'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/lib/passenger/
    

    abstract_server.rb:135:in start'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/lib/passenger/
    

    abstract_server.rb:112:in fork'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/lib/passenger/
    

    abstract_server.rb:112:in start'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/lib/passenger/
    

    railz/framework_spawner.rb:87:in start'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/lib/passenger/
    

    spawn_manager.rb:224:in spawn_rails_application'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/lib/passenger/
    

    spawn_manager.rb:219:in synchronize'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/lib/passenger/
    

    spawn_manager.rb:219:in spawn_rails_application'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/lib/passenger/
    

    spawn_manager.rb:122:in spawn_application'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/lib/passenger/
    

    spawn_manager.rb:253:in handle_spawn_application'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/lib/passenger/
    

    abstract_server.rb:317:in __send__'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/lib/passenger/
    

    abstract_server.rb:317:in main_loop'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/lib/passenger/
    

    abstract_server.rb:168:in start_synchronously'

     /usr/local/lib/ruby/gems/1.8/gems/passenger-2.0.1/bin/passenger-
    

    spawn-server:46

    My version of your code:

       def project_list
    
        @cats = Cat.find(params[:id])
        @projects = @cats.projects.paginate :page => params
    

    [:page], :per_page => 4

       end
    

    Processing PortfolioController#project_list (for 207.237.77.240 at 2008-08-10 16:35:11) [GET] Session ID: d32eda2e277f64a1a2455b03d4836c41 Parameters: {"action"=>"project_list", "id"=>"2", "controller"=>"portfolio"} Rendering within layouts/portfolio Rendering portfolio/project_list DEPRECATION WARNING: @request is deprecated! Call request.relative_url_root instead of @request.relative_url_root. Args: [] See http://www.rubyonrails.org/depre... for details. (called from url_for_file_column at /home/maggie/portfolio/vendor/ plugins/file-column-0.3.1/lib/file_column_helper.rb:62) DEPRECATION WARNING: @request is deprecated! Call request.relative_url_root instead of @request.relative_url_root. Args: [] See http://www.rubyonrails.org/depre... for details. (called from url_for_file_column at /home/maggie/portfolio/vendor/ plugins/file-column-0.3.1/lib/file_column_helper.rb:62) DEPRECATION WARNING: @request is deprecated! Call request.relative_url_root instead of @request.relative_url_root. Args: [] See http://www.rubyonrails.org/depre... for details. (called from url_for_file_column at /home/maggie/portfolio/vendor/ plugins/file-column-0.3.1/lib/file_column_helper.rb:62) DEPRECATION WARNING: @request is deprecated! Call request.relative_url_root instead of @request.relative_url_root. Args: [] See http://www.rubyonrails.org/depre... for details. (called from url_for_file_column at /home/maggie/portfolio/vendor/ plugins/file-column-0.3.1/lib/file_column_helper.rb:62) DEPRECATION WARNING: @request is deprecated! Call request.relative_url_root instead of @request.relative_url_root. Args: [] See http://www.rubyonrails.org/depre... for details. (called from url_for_file_column at /home/maggie/portfolio/vendor/ plugins/file-column-0.3.1/lib/file_column_helper.rb:62) DEPRECATION WARNING: @request is deprecated! Call request.relative_url_root instead of @request.relative_url_root. Args: [] See http://www.rubyonrails.org/depre... for details. (called from url_for_file_column at /home/maggie/portfolio/vendor/ plugins/file-column-0.3.1/lib/file_column_helper.rb:62) Completed in 0.20623 (4 reqs/sec) | Rendering: 0.14943 (72%) | DB: 0.00698 (3%) | 200 OK [http://www.portfolio.maggiereid.... project_list/2]

  • Mislav

    Mislav August 11th, 2008 @ 04:01 AM

    That isn't my code if it breaks. You re-typed it wrong, that's why you have a syntax error. But OK, you fixed it by yourself.

    I'm not getting why aren't there SQL queries logged in development mode. Has it been turned of somehow?

  • Maggie

    Maggie August 13th, 2008 @ 06:50 PM

    Hi Mislav,

    This is the code that finally worked.

    def project_list

       @cats = Cat.find(params[:id])
       @projects = Project.find(:all, :conditions => ["cat_id = ?",
    

    params[:id]])

       @projects = Project.paginate :page => params
    

    [:page],:conditions => ["cat_id = ?", params[:id]], :order => "date DESC", :per_page =>2

          end
    

    Thanks for all your help with this. I really appreciate it. Maggie

    On Aug 10, 2008, at 10:02 PM, Lighthouse wrote:

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Everyone's favorite Ruby library for pagination of practically anything!

People watching this ticket

Pages