Pagination with nested resource
luvcjssy opened this issue · 2 comments
luvcjssy commented
I have an issue with nested resource. Here is example:
p = Products.first
p.steps.count ---> 10
a = p.steps.paginate(page: 1, per_page: 2)
a.count ---> 10
but,
a = p.steps.paginate(page: 1, per_page: 2).to_a
a.count ---> 2
So, which case is correct?
thorvn commented
I have the same issue.
timkrins commented
Depends what you want to be counting.
The p.steps.paginate(page: 1, per_page: 2).count
call will call the count
method on the underlying ActiveRecord::AssociationRelation
The p.steps.paginate(page: 1, per_page: 2).to_a.count
call will execute the paginate
method when the to_a
method is called, and then the count
method on the Array
.
As for 'correctness'... it states in the README:
# paginate in Active Record now returns a Relation
But paginate
must be changing the to_a
result.