Pagination for an object array

def show_details

@sd_ticket = ServiceDeskTicket.find(params[:id])
@servicedesk_cis = @sd_ticket.service_desk_cis

end

What I need is a pagination for @servicedesk_cis using the custom
pagination…How can i give offset and limit to this?

I have successfully done previously custom pagination in another def as
below

def search_sd_ticket
@search_sd_ui_hash=params[:sd_ticket]
@sd_ticket_number=@search_sd_ui_hash[:number]
@sd_ticket_name = @search_sd_ui_hash[:name]

      # 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
|sd|
sd.number.downcase =~
“%”+@sd_ticket_number.downcase+"%" if !@sd_ticket_number.nil?
sd.name== @sd_ticket_name

                             end

        record_count = @search_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 = Paginator.new(self,
record_count, items_per_page, page)

        # step 4: only find the requested subset of

@search_sd_ticket_result
@search_sd_ticket_result =
ServiceDeskTicket.find_where(:all,:offset=>offset,:limit=>items_per_page
) do |sd|
sd.number.downcase =~
“%”+@sd_ticket_number.downcase+"%" if !@sd_ticket_number.nil?
sd.name== @sd_ticket_name
end
end

But here i directly gave offset and limit in find_where.So how can i
apply this to my case?ie,

@servicedesk_cis = @sd_ticket.service_desk_cis #suppose contain 100
total records and i need as in my suucessful case 20 records per page…

Please help
Sijo

Hi Sijo

I’m a bit confused by your example (ie too lazy to read it all
properly), but what i usually do for custom pagination is to do a custom
query to get the ids of the records i want, put them (the ids) in an
array, and then do a paginated query for records whose id’s are in the
array.

eg

@records = Record.paginate(
:conditions => [“id in (?)”,
all_the_records_i_want_to_paginate.collect(&:id)],
:page => params[:page] || 1,
:per_page => 15)

Effectively you do two queries: one that gets everything, and then
another that paginates across it. So, it’s a bit clumsy maybe but you
can often get the array of all required ids relatively efficiently.