Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pagination with nested resource #574

Open
luvcjssy opened this issue May 18, 2018 · 2 comments
Open

Pagination with nested resource #574

luvcjssy opened this issue May 18, 2018 · 2 comments

Comments

@luvcjssy
Copy link

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?

@th0j
Copy link

th0j commented May 18, 2018

I have the same issue.

@timkrins
Copy link

timkrins commented Jun 19, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants