Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6856907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:56:51+00:00 2026-05-27T01:56:51+00:00

I’m having a big problem with will paginate. I have a query defined as

  • 0

I’m having a big problem with will paginate.

I have a query defined as scope in my model:

scope :published_and_valid, joins(...).where(...)

In my controller I sometimes just want to have the top 20.

.limit(20)

Now I want to be able to paginate over these 20 results (5 per page)

And I try to do this:

@articles = Article.published_and_valid.limit(20).paginate(:page => params[:page])

This does not work as the result in my view is a pagineted result for ALL articles

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:56:52+00:00Added an answer on May 27, 2026 at 1:56 am

    It looks like will_paginate imposes its own limit clause on your relation in order to do the paging. After some experimentation, the easiest way I could find to do this is to do eager-loading of the result set and use the functionality contained in the array extensions to do the actual pagination.

    Here’s a quick example; note that you have to require will_paginate/array; you may wish to do this in an initializer or somewhere else. Note that it does monkey-patch Array to have the paginate method.

    require 'will_paginate/array'
    
    class ArticlesController < ApplicationController
      def index
        @articles = Article.published.limit(20).all # call `all` to eager load
        @articles = @articles.paginate(:page => params[:page], :per_page => 10)
      end
    end
    

    [Update]

    Another option is to pass in the total_entires option to will_paginate based on the result set size.

    class ArticlesController < ApplicationController
      def index
        @articles = Article.published.limit(20)
        @articles = @articles.paginate(:page => params[:page], :per_page => 10, :total_entries => @articles.count)
      end
    end
    

    You can’t pass in 20 as you can’t guarantee that a full 20 results will be returned; by passing in the count attribute, you can ensure that the pagination will be correct.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.