# Pagination for an object array

**URL:** <https://www.ruby-forum.com/t/pagination-for-an-object-array/131122>\
**Category:** Rails\
**Created:** [March 6, 2008, 7:34am UTC](https://www.ruby-forum.com/t/pagination-for-an-object-array/131122 "2008-03-06T07:34:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [March 6, 2008, 7:34am UTC](https://www.ruby-forum.com/t/pagination-for-an-object-array/131122/1 "2008-03-06T07:34:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [March 7, 2008, 5:33pm UTC](https://www.ruby-forum.com/t/pagination-for-an-object-array/131122/2 "2008-03-07T17:33:22Z")

</div>

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.
