# Moving pagination code to model

**URL:** <https://www.ruby-forum.com/t/moving-pagination-code-to-model/135924>\
**Category:** Rails\
**Created:** [April 26, 2008, 11:42am UTC](https://www.ruby-forum.com/t/moving-pagination-code-to-model/135924 "2008-04-26T11:42:26Z")\
**Posts on this page:** 1\
**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:** [April 26, 2008, 11:42am UTC](https://www.ruby-forum.com/t/moving-pagination-code-to-model/135924/1 "2008-04-26T11:42:26Z")

</div>

Hi  
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  
|sd|

```
                             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|

```
                             end

```

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

Sijo
