我只是在使用 Ruby on Rails 3.0 和一个简单的留言板,发现了 will_paginate 的几个问题。
最紧迫的是,每次显示新页面时,都会对主题中的每个帖子执行数据库查询。
正如您可以想象的,如果您的主题包含 10,000 多个帖子,速度会非常慢。
有办法阻止这种奇怪的行为吗?
显示 Controller :
@posts=@topic.posts
@posts = Post.paginate @posts, :page => params[:page],:order => "post_number"
型号
cattr_reader :per_page
@@per_page = 20
查看
<%= will_paginate @posts %>
最佳答案
在你的 Controller 中尝试:
@posts = Post.paginate_by_topic_id @topic.id, :page => params[:page],:order => "post_number"
查看 will_paginate docs 中的示例
关于ruby-on-rails-3 - 如何阻止 will_paginate 从数据库获取每个帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3647038/