Question

I have an array of objects in my controller, paginated by Kaminari:

@pics = Pic.page(params[:page]).per(12).order(sort_column)

I render each new set of 12 pics on the same page with an AJAX request, for a manual infinite scroll effect. The sort_column bit sorts the pics according to most_viewed attribute within the object.

My problem - sometimes, when there are a number of pics with the same most_viewed attribute, - i.e. there are 10 images with "20" as their most_viewed value - I will get repeated objects when the new set of 12 is revealed with the AJAX request.

Here's an example of what I mean... I've clicked the "load more" (i've left it in to demonstrate), and there are objects repeated when the new batch loads.

repeated objects on Kaminari

Any help would be much appreciated. Thanks in advance.

Was it helpful?

Solution

You have to order on count AND another column (id would be the best), in this way SQL will returns a list that will not change (because right now SQL does not know what to do if X records have the same count).

You can change .order(sort_column) to .order("#{sort_column} ASC, id ASC"), and it should work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top