find_by_sql and pagination with kamanari

3.8k Views Asked by At

I have a model that connects to an external database and I query using the find_by_sql method like so:

External.find_by_sql("SELECT * from table")

However when I add the .page(params[:page]), the error "undefined method page for class array" appears. Is it possible to paginate the results fetched using find_by_sql?

3

There are 3 best solutions below

2
On BEST ANSWER

If you cannot avoid using find_by_sql, check out last question of kaminari wiki.

1
On

This worked for me:

Kaminari.paginate_array(my_array_object).page(params[:page]).per(10)

https://github.com/kaminari/kaminari/wiki/Kaminari-recipes#-how-do-i-paginate-an-array

0
On

Looking at the kaminari plugin it seems to only apply to ActiveRecord and ActiveRecord scopes, but find_by_sql does not work as a scope, but rather returns an Array.

I haven't found, digging through kaminari, a transparent way to 'paginate' an array. You could try to roll it by hand, but it might be tricky.