S
S
Stergy2019-06-20 19:00:04
Ruby on Rails
Stergy, 2019-06-20 19:00:04

How to write pagination using limit and offset?

There is an API written in RoR using the Grape gem.
Looks like this:

class V1::Authenticated::PlanActions < Grape::API
  resource :tickets do

    get serializer: TicketSerializer do
      available_tickets = Base.accessible_by(@ability).map(&:id)
      ticket = Ticket.where(available_tickets_id: available_tickets)
      ticket
    end
  end
end

Returns something like this JSON
[
  {
    "name": "MP-3",
    "updated_at": "2019-06-19T14:18:27.371Z",
    "review_status": "Not Started"
  },
  {
    "name": "Co-1",
    "updated_at": "2019-06-19T14:40:55.372Z",
    "review_status": "Complete"
  },
  {
    "name": "",
    "updated_at": "2019-06-20T12:31:54.534Z",
    "review_status": "In Progress"
  },
  {
    "name": "CB-4e",
    "updated_at": "2019-06-20T12:37:46.838Z",
    "review_status": "Complete"
  },
  {
    "name": "Coun62",
    "updated_at": "2019-06-20T13:30:47.673Z",
    "review_status": "Not Started"
  },
  {
    "name": "5556jjj",
    "updated_at": "2019-06-20T13:43:12.527Z",
    "review_status": "Not Started"
  }
]

From the front they come params[page, per_page]
where page is the desired page, and per_page is the number of tickets on the page.
I know that this can be done with ready-made solutions like Kaminari.
Well, I need to implement this manually, preferably using limt, offence.
Please tell a beginner how to implement this, what to read, in which direction to move?
Thank you in advance

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
oh_shi, 2019-06-20
@Stergy

Ticket.limit(params[:per_page]).offset((params[:page].to_i - 1) * params[:per_page])

T
TyzhSysAdmin, 2019-06-20
@POS_troi

Read any articles and see any example on the topic "writing a blog on %langname%" or in Google "%database name% pagination"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question