How to stop will_paginate from getting each record from the database

I just play with Ruby on Rails 3.0 with a simple bulletin board and have found a few problems with will_paginate.

The most important thing is that every time a new page is displayed, a query is made to the database of each individual message in the subject.

As you can imagine, if you have a topic with 10,000 + posts, this is very slow.

Is there any 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 %>
+3
source share
2 answers

In the controller, try:

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

Take a look at the example in will_paginate docs

+1
source

_ 3.0.0. :

class Post
  self.per_page = 20
end

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

Source: https://habr.com/ru/post/1763346/


All Articles