0

i want to display the 3 last looks in the first row, then i want to display all others but without the 3 lasts looks.

This is my code

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

the problem is if i put offset like below, i've got the same 8 looks at every pages, how to handle that please ?

@looks = Look.search query, where: conditions, order: { created_at: :desc }, page: params[:page], per_page: 8, offset: 3
end
大陸北方網友
  • 3,696
  • 3
  • 12
  • 37

2 Answers2

1

simply exclude the first 3 results from the @looks (check 6th line)

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
blackbiron
  • 809
  • 10
  • 17
0

Seems like you want to calculate the offset, something like:

  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

But you can also consider using kaminari gem which will make the calculation for you. Seems like Searchkick allows that.

Also, see this answers.

Alexander
  • 1,072
  • 1
  • 5
  • 9
  • thanks, i tried your code but it doenst work, i'm already using kaminari. Wen is use offset on searchick it removed the last 3 looks, but when i change page it show again and again the same looks per page .... i just want the offset on the Look.all => just remove 3 looks then display 8 looks per page – Benoît Bargès Oct 09 '20 at 11:09
  • Can you tell me what doesn't work? What's the error? Also, does your page count starts at 1? (or maybe at 0?) – Alexander Oct 09 '20 at 11:52
  • Ok i'm trying to explain the simpliest (sorry for my english) : - On Look#index, only on page 1 i want to display the 3 newest looks, represented by "@last_3_looks" (works fine) -then i dont want to have these 3 looks in "@looks", and want to display 8 looks per page with kaminari, the problem is if i put offset: 3 in the query, they are no longer here but if i go to next page it's always the same 8 looks displaying, if i move it i still have the 3 last looks in "@looks", but pagination works fine... very struggling .... – Benoît Bargès Oct 09 '20 at 12:04