undefined method `page' for Array with Will paginate 3.0.3 #263

Closed
Azzurrio opened this Issue Sep 4, 2012 · 1 comment

Comments

Projects
None yet
2 participants

Azzurrio commented Sep 4, 2012

I'm working with jQuery datatable gem and I want to sort specific column by calculated data, so I defined a method in product model called available_hours and then I used sort_by to get sorted objects as following:

products = current_user.products.sort_by {|p| p.available_hours.to_s}
products = products.page(page).per_page(per_page)

The first line above is working fine and it's returning an array of products object, but the problem come form second line with will_paginate in this error

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

I searched for this error and I found that I need to require this line

require 'will_paginate/array'

so I added it to application.rb

require 'rails/all'
require 'will_paginate/array'

but it didn't work, also I tried to add it to environment.rb

SuperMarket::Application.initialize!
require 'will_paginate/array'

and it didn't work too, so where should I require this line specifically ?

Owner

mislav commented Jan 10, 2013

The page() method is only available on Active Record Relations. When you called sort_by in your code, however, you've triggered the SQL query and got the results in form of a ordinary Array. In other words, it stopped being a relation. You can require "will_paginate/array" and use the paginate(:page => num, :per_page => num) method.

mislav closed this Jan 10, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment