mtay
mtay

Reputation: 1316

will pagination - passing in an array variable

I have a very specific list of items I'd like to paginate.

Up to now, I've been doing

@company = Company.paginate( :page => params[:page], 
                               :conditions => ["activated=1... etc..."],
                               :per_page => 6,
                               :order => order_by)

But I'd like to create my own array @company that wouldn't be possible to retrieve through a single query.. and then paginate that. How can I pass the raw array to will_paginate?

Thank you...

Upvotes: 0

Views: 373

Answers (2)

Harish Shetty
Harish Shetty

Reputation: 64363

Call the paginate method on the array.

a = [1,2,3,4,5]

a.paginate(:page => 1, :per_page => 2)
=> [1,2]    

a.paginate(:page => 2, :per_page => 2)
=> [3,4]    

a.paginate(:page => 3, :per_page => 2)
=> [5]

Reference: The Array extension in the will_paginate gem source.

Upvotes: 1

Mori
Mori

Reputation: 27779

When loaded, will_paginate extends all arrays with the paginate method:

ruby-1.9.2-p180 :014 > [].respond_to?(:paginate)
=> true 

You can just create your array instance however you want and call paginate on it.

Upvotes: 2

Related Questions