2

I'm trying to implement pagination in my Rails 3 app with Kaminari but I'm getting the following error:

undefined method `page' for #<Array:0x007fe43f4b0e80>

This is what I have in my controller:

@stories = Story.find_all_by_keynote_id(@keynote, :order => 'created_at DESC').page(params[:page])

And this is what I put in my view:

<%= paginate @stories %>

I think there's a problem with the "find_all_by_keynote_id" but I'm not sure how to fix it.

Thank you!

Felipe Cerda
  • 480
  • 1
  • 5
  • 20

3 Answers3

5

Kaminari can also paginate arrays (which is what you have)

array = Story.find_all_by_keynote_id(@keynote, :order => 'created_at DESC')
@stories = Kaminari.paginate_array(array).page(params[:page])
LanguagesNamedAfterCofee
  • 5,782
  • 7
  • 45
  • 72
  • 1
    Thanks! I didn't know that. I used another approach but with the same result. I put this in my controller `@stories = Story.where("keynote_id = ?", params[:keynote_id]).order("created_at DESC").page(params[:page])` – Felipe Cerda Nov 21 '11 at 01:08
  • 1
    I get an undefined method "paginate_array" for Kaminari:module how to solve this? – Þaw Jan 29 '14 at 01:28
3

Yes, but how to query the same without an array? That's what I need.

@stories = Story.where(:keynote_id => @keynote).order('created_at DESC').page(params[:page])

or

Keynote.has_many :stories

@stories = @keynote.stories.order('created_at DESC').page(params[:page])
Akira Matsuda
  • 1,854
  • 14
  • 9
2

You are feeding the page method an array when it is expecting an Active Record Relation. If you aren't familiar with that term then you should have a look at the active record query guide.

According to the Kaminari docs this is how you are supposed to use it:

@stories = Story.order(:created_at).page(params[:page])
sosborn
  • 14,676
  • 2
  • 42
  • 46