Rails 4 will_paginate repeated results

ruby-on-rails, will-paginate

Solution

Hopefully not digging up old memories, but..

I also have come across this issue. For me it was caused by using PostgreSQL - my solution was to add an additional sort_by clause for id.

Changing

`events.order(:start_date, :series_id).paginate(page: params[:page], per_page: 12)`

to

`events.order(:start_date, :series_id, :id).paginate(page: params[:page], per_page: 12)`

And everything was right in the world, again.

Issue raised and answered on will_paginate: https://github.com/mislav/will_paginate/issues/420

Problem

One of my index pages shows the same resource on multiple pages with `will_paginate`. I have other pages that are paginating perfectly and can't figure out what's wrong. In my model I have this method: ``` self.per_page = 10 def self.index_search(query) if query.present? self.approved.where("name ilike :q or ko_name ilike :q", q: "%#{query}%") .order(date: :asc).group("id") else approved.upcoming.order(date: :asc).group("id") end end ``` Then in the controller: ``` def index @events = Event.index_search(params[:query]).paginate(page: params[:page]) end ``` And in the view: ``` <%= will_paginate @events, renderer: BootstrapPagination::Rails %> ``` Typically, the last item on the first page is repeated in the same spot on the second page. I added the `.group("id")` to the method after digging around on StackOverflow, but I'm stumped at what I should be looking at next. edit: It appears if I change `self.per_page = 10` to a number greater than 12, the repeat issue goes away. I would really like to keep it at 10 per page. Edit: it may also be relevant that all of the events have a `date` field and the paginated events all have the same date value.

Original source