Moving pagination code to model

I have the controller code as below

step 1: set the variables you’ll need

        page = (params[:page] ||= 1).to_i
        items_per_page = 20
        offset = (page - 1) * items_per_page
       # step 2: instead of performing the full find, just find the

record count

        @search_sd_ticket_count_result =

ServiceDeskTicket.find_where(:all,:select=>‘count(*) as count1’ ) do


        [email protected]_sd_ticket_count_result[0].count1.to_i

        # step 3: create a Paginator, the second variable has to be

the number of ALL items on all pages
@search_sd_ticket_result_pages =,
record_count, items_per_page, page)

        # step 4: only find the requested subset of

@search_sd_ticket_result =
) do |sd|


How can I move this code to model so that to call it other places also


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs