views:

59

answers:

1

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 %>
+1  A: 

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

danivovich
paginate_by_topic_id is undefined, in fact there are no paginate_by_xxx methods. The only pagination methods that respond are: "paginate", "paginate_by_sql", "paginated_each". This is using 3.0.pre2 which seems to be the newest version.
Does Post not `belong_to :topic` with a `topic_id` column?
danivovich
I solved it by using paginate_by_sql.
Is the paginate_by_* methods supposed to be mixed into the models?
I haven't used the latest version of will_paginate, but it looks like the docs are saying all the standard find_by_* methods are available as paginate_by_*, so as long as topic_id is a column, it should be available.
danivovich
Alright, I guess I will try to bug it. Thank you. You got pointed in the right direction, I would +1 your answer but it won't let me.