分页、限制和偏移问题、rails

g0czyy6m  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(2)|浏览(491)

我想在第一行显示最后3个外观,然后我想显示所有其他外观,但没有最后3个外观。
这是我的密码

def index
    query = params[:query].presence || "*"
    conditions = {}
    conditions[:available] = true
    conditions[:look_tags] = params[:look_tags] if params[:look_tags].present?
    @last_3_looks = Look.search query, where: conditions, order: { created_at: :desc }, limit: 3
    @looks = Look.search query, where: conditions, order: { created_at: :desc }, page: params[:page], per_page: 8
end

问题是,如果我把偏移量如下,我有同样的8看每一页,如何处理呢?

@looks = Look.search query, where: conditions, order: { created_at: :desc }, page: params[:page], per_page: 8, offset: 3
end
hsvhsicv

hsvhsicv1#

好像你想计算偏移量,比如:

page = params[:page].to_i
  per_page = 8
  starting_offset = 3
  offset = starting_offset + ((page - 1) * per_page)

  @looks = Look.search query, where: conditions, order: { created_at: :desc }, page: page, per_page: per_page, offset: offset

但你也可以考虑使用卡米纳里宝石,这将使你的计算。似乎searchkick允许这样做。
另外,请看下面的答案。

rjee0c15

rjee0c152#

只需从@looks中排除前3个结果(勾选第6行)

query = params[:query].presence || "*"
conditions = {}
conditions[:available] = true
conditions[:look_tags] = params[:look_tags] if params[:look_tags].present?
@last_3_looks = Look.search query, where: conditions, order: { created_at: :desc }, limit: 3
conditions[:id] = { not: @last_3_looks.results.collect(&:id) }
@looks = Look.search query, where: conditions, order: { created_at: :desc }, page: params[:page], per_page: 8

相关问题