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

Duplicate items with a has_many association #495

Open
brettcave opened this issue Jul 30, 2016 · 2 comments
Open

Duplicate items with a has_many association #495

brettcave opened this issue Jul 30, 2016 · 2 comments

Comments

@brettcave
Copy link

Not 100% sure of where the issue is, but it seems to come up with has_many associations.

If I have a scope in a Foo model (where a user has_many: foos) - models/foo.rb:

belongs_to :user
scope :hasCars, -> { joins(:cars) }

And now try use the scope with will_paginate (in the controller):

@foos = auser.foos.hasCars.paginate(:page=>1,:per_page=>20)

Then the view duplicates each foo by the number of cars they have in the :through ownership association - so if auser.foos.first has 3 cars. foos.first will be iterated through 3 times in the paginated view.

I can work around this by using a group:

@foos = auser.foos.hasCars.group("car_id").paginate

http://stackoverflow.com/questions/38671915/rails-will-paginate-with-named-scope-for-associations-0/38671965#38671965

http://stackoverflow.com/questions/11729816/rails-will-paginate-shows-duplicates-on-habtm-models

@yangez
Copy link

yangez commented Nov 7, 2016

👍 also experiencing this

@LPBR
Copy link

LPBR commented May 17, 2017

Another workaround is to order the query by id.
https://github.com/kaminari/kaminari/issues/665

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