Rails 3,will_paginate 超过现有的有限活动记录结果

发布于 2024-12-17 03:44:01 字数 397 浏览 2 评论 0原文

我在分页方面遇到了很大的问题。

我的模型中有一个定义为范围的查询:

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

在我的控制器中,我有时只想获得前 20 个结果。

.limit(20)

现在我希望能够对这 20 个结果进行分页(每页 5 个)

,我尝试这样做:

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

这确实不起作用,因为在我看来,结果是所有文章的分页结果

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

困倦 2024-12-24 03:44:01

看起来 will_paginate 在您的关系上强加了自己的 limit 子句来进行分页。经过一些实验,我发现执行此操作的最简单方法是立即加载结果集并使用 数组扩展 来执行实际的分页。

这是一个简单的例子;请注意,您必须 require will_paginate/array;您可能希望在初始化程序或其他地方执行此操作。请注意,它对 Array 进行了猴子补丁以使其具有 paginate 方法。

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

[更新]

另一种选择是根据结果集大小将 total_entires 选项传递给 will_paginate。

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

你不能传入20个,因为你不能保证返回完整的20个结果;通过传入 count 属性,您可以确保分页正确。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文