ruby on rails - Displaying multiple copies of one activerecord object -


i displaying list of articles , using will_paginate gem , endless scrolling. working fine.

my problem, however, getting multiple copies of same article in view. doesn't happen time, , nor every article, enough bloody annoying.

i know there no duplication in database, , problem occurs in rendering somehow.

here controller:

@articles = article.order("date_published desc", "random()").paginate(:page => params[:page],:per_page => 10)   

i trying somehow include distinct or uniq method, seems can't use either order method.

i using pg db.

any appreciated!

the issue arises sub-sort, random().

imagine have these posts:

id  published  title ----------------------  1  1/1/1970   1  2  1/2/1970   2  3  1/2/1970   3  4  1/3/1970   4 

if request page 1 (with page size of 2), can either 4 , 3, or 4 , 2. now, if request page 2 can either 3 , 1 or 2 , 1. because of random() sub-sort. first query may pull page result:

id  published  title ----------------------  4  1/3/1970   4  3  1/2/1970   3  2  1/2/1970   2  1  1/1/1970   1 

which 4 , 3, result possible:

id  published  title ----------------------  4  1/3/1970   4  2  1/2/1970   2  3  1/2/1970   3  1  1/1/1970   1 

which 4 , 2. , either of these results result in different page 2 well. random sort cause of duplicates, need use consistent sorts or manage removal of duplicates in other way. such as:

@articles = article.where.not(id: list_of_already_fetched_ids).order("date_published desc", "random()").paginate(:page => params[:page],:per_page => 10) 

note: where.not usable in rails 4, if not using rails 4 you'll need find way implement filter.


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -