Skip to content

Duplicate items with a has_many associationΒ #495

Open
@brettcave

Description

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

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions