[英]Rails 4 will_paginate repeated results
One of my index pages shows the same resource on multiple pages with will_paginate
. 我的索引页面之一在具有
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. 在对StackOverflow进行深入研究之后,我将
.group("id")
到了该方法中,但是我为接下来要看的内容感到困惑。
edit: 编辑:
It appears if I change self.per_page = 10
to a number greater than 12, the repeat issue goes away. 如果我将
self.per_page = 10
更改为大于12的数字,则会出现重复问题。 I would really like to keep it at 10 per page. 我真的很想保持在每页10张。
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. 编辑:所有事件都具有
date
字段,而分页事件都具有相同的日期值也可能是相关的。
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.
对我来说,这是由使用PostgreSQL引起的-我的解决方案是为id添加一个额外的sort_by子句。
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 在will_paginate上提出并回答的问题: https : //github.com/mislav/will_paginate/issues/420
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.