开发者

How to stop will_paginate from getting every post from the database

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 i开发者_如何学编程s 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:

@posts=@topic.posts
@posts = Post.paginate @posts, :page => params[:page],:order => "post_number"

Model

cattr_reader :per_page 
@@per_page = 20

view

<%= will_paginate @posts %>


In your controller try:

@posts = Post.paginate_by_topic_id @topic.id, :page => params[:page],:order => "post_number"

Look at the example in the will_paginate docs


Upgrade will_paginate to version 3.0.0. Then:

class Post
  self.per_page = 20
end

@topic.posts.page(params[:page]).order("post_number")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜