如何阻止 will_paginate 从数据库获取每个帖子
我只是在使用 Ruby on Rails 3.0 和一个简单的留言板,发现了 will_paginate 的几个问题。
最紧迫的是,每次显示新页面时,都会对主题中的每个帖子执行数据库查询。
正如您可以想象的,如果您的主题有 10,000 多个帖子,那么速度会非常慢。
有没有办法阻止这种奇怪的行为?
显示控制器:
@[email protected]
@posts = Post.paginate @posts, :page => params[:page],:order => "post_number"
模型
cattr_reader :per_page
@@per_page = 20
视图
<%= will_paginate @posts %>
I am just playing around with Ruby on Rails 3.0 with a simple message board and found several issues with will_paginate.
The most pressing is that each time a new page is displayed a database query of every single post in the topic is performed.
As you can imagine, if you have a topic with 10,000+ posts this is very slow.
Is there a way to stop this odd behavior?
Show controller:
@[email protected]
@posts = Post.paginate @posts, :page => params[:page],:order => "post_number"
Model
cattr_reader :per_page
@@per_page = 20
view
<%= will_paginate @posts %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的控制器中尝试:
查看 will_paginate 文档 中的示例
In your controller try:
Look at the example in the will_paginate docs
将 will_paginate 升级到版本 3.0.0。然后:
Upgrade will_paginate to version 3.0.0. Then: