'Using Pagy with Filtered Data
@animals = @animals.select{|animal| animal.temporary_home == home_params }
@animals = .... additional filters
@pagy, @animals = pagy(@animals)
I'm new to pagy and I keep running into this error when I attempt to apply filtering to the data
17:23:03 web.1 | NoMethodError (undefined method `offset' for []:Array):
If I do not apply filtering, the following works:
@pagy, @animals = pagy(Animal.all)
Is it possible to use Pagy with filtered data?
Solution 1:[1]
So the problem is pagy actually works on ActiveRecord::Collection since @animals is an array the methods for ActiveRecord::Collection wont be present on it.
Pagy includes another extention (?) to work with arrays. check out
https://ddnexus.github.io/pagy/extras/array.html#gsc.tab=0
require 'pagy/extras/array'
@animals = @animals.select{|animal| animal.temporary_home == home_params }
@animals = .... additional filters
@pagy, @animals = pagy_array(@animals)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nijeesh Joshy |
